【问题标题】:Firebase: put collection inside an arrayFirebase:将集合放在数组中
【发布时间】:2020-10-26 12:33:44
【问题描述】:

有人已经在 Angular 上使用了 ng-table,并且可以弄清楚如何将它与 firebase 集合一起使用?

我正在尝试使用ng-table(最后一个示例)在 Angular 10 上对我的集合进行排序和过滤,因此我尝试将代码中的常量更改为我的对象,但它没有用。我想我需要将我的对象转换为一个数组,所以我尝试了this library 来做到这一点,也没有工作。还试图在 stackoverflow 上寻找这个,我发现的只是其他有同样问题的人。

这是我需要执行此操作的确切功能。 COUNTRIES 是来自 *countries.ts 的数组。 country.service.ts 上的功能如下。

  private _search(): Observable<SearchResult> {
    const {sortColumn, sortDirection, pageSize, page, searchTerm} = this._state;

    // 1. sort
    let countries = sort(COUNTRIES, sortColumn, sortDirection);

    // 2. filter
    countries = countries.filter(country => matches(country, searchTerm, this.pipe));
    const total = countries.length;

    // 3. paginate
    countries = countries.slice((page - 1) * pageSize, (page - 1) * pageSize + pageSize);
    return of({countries, total});
  }
}

这就是我对发现的库的尝试:

this.items = this.firestore.collection('users').valueChanges();

alert( O2A(this.items) );

但这对我不起作用。

谢谢!

【问题讨论】:

  • 你什么时候从你的服务中调用 _search() ?您尝试使用 O2A 方法实现什么目标? ValuesChanges 已经返回了一个包含数组的 observable。
  • 我收到此错误:“Observable”类型缺少“User[]”类型中的以下属性:长度、弹出、推送、连接等 25 个。
  • 你得到这个是因为它是一个被返回的 observable。它是一个异步对象,在订阅时,每次更新时都会发出集合中的数据(在您的 firebase 集合上创建、更新、删除)。您首先需要订阅它以通过执行以下操作来请求数组:this.items.subscribe(res =&gt; alert(O2A(res)))。不需要的时候别忘了取消订阅,否则会在应用中造成数据泄露,严重影响应用性能。
  • 仍然出现错误。您能否获得一个完整的示例来说明如何解决这个问题并更改 ng-table 上的 firebase 集合的示例数据?
  • 你能在我发表评论后用你试图实现的目标来编辑你的问题吗?我对您的最终目标和实施一无所知。您的两段代码似乎与我无关。它们是如何相互作用的?

标签: angular typescript firebase google-cloud-firestore ng-bootstrap


【解决方案1】:

我会以你提供的例子来回答。
这个工作的最低要求是稍微调整 country.service。我们对constructor()private _search() 方法进行了更改。我们需要将包含数据数组的 COUNTRIES 变量替换为您的用户数组(请注意,您需要调整所有代码以匹配您的用户属性):

country.service.ts

...
users: User[];

constructor(private pipe: DecimalPipe) {
    this._search$.pipe(
      tap(() => this._loading$.next(true)),
      debounceTime(200),
      switchMap(() => this._search()),
      delay(200),
      tap(() => this._loading$.next(false))
    ).subscribe(result => {
      this._countries$.next(result.users);
      this._total$.next(result.total);
    });
    // this will get your users and start the _search$ above
    this.firestore.collection('users').valueChanges().pipe(take(1)).subscribe(users => {
        this.users = users;
        this._search$.next();
    });
  }
  
  private _search(): Observable<SearchResult> {
    const {sortColumn, sortDirection, pageSize, page, searchTerm} = this._state;

    // 1. sort
    // provide your users (instead of COUNTRIES exemple variable) as a param 
    let users = sort(this.users, sortColumn, sortDirection);

    // 2. filter
    users = users.filter(user => matches(user, searchTerm, this.pipe));
    const total = users.length;

    // 3. paginate
    users = users.slice((page - 1) * pageSize, (page - 1) * pageSize + pageSize);
    return of({users, total});
  }
...

不要忘记在方法参数中相应地重命名变量(users 而不是 countries)和类型(User 而不是 Country)。

我不会做所有的工作,因为它太宽泛了,但至少,您知道如何使用您的 Firestore 数据启动服务。

还要更改 SearchResult 接口,否则会出现编译错误:

interface SearchResult {
  users: User[];
  total: number;
}

【讨论】:

  • 非常感谢!看起来这对我来说是解决方案。但我的“take”功能有问题,出现此错误:找不到名称“take”。 89 this.firestore.collection('users').valueChanges().pipe(take(1)).subscribe(users => { ~~~~ src/app/pages/users/item.service.ts:90: 9 - 错误 TS2740:类型“{}”缺少类型“用户 []”中的以下属性:长度、弹出、推送、连接以及另外 26 个。
  • Take 是一个 rxjs 运算符。它将进行第一次发射并完成 observable(您只需要一个请求即可获取数据)。但是您需要导入它才能使用它。为此,请将 import { take } from 'rxjs/operators'; 与其他导入一起输入。
  • 好的,谢谢!我想我快到了。我现在只得到我以前得到的错误。我确实从 import {User} from './item' 导入我的界面;但仍然出现此错误:类型“未知 []”不可分配给类型“用户 []”。类型“{}”缺少“用户”类型的以下属性:id、bio、birthday、createdDate 等 15 个。 90 this.users = 用户;
  • 我通过 this.firestore.collection('users') 更改了 this.firestore.collection('users') 并修复了这个错误,所以我把它放在 console.log 和像你说的那样得到错误,但仍然没有在桌子上加载信息
  • 这样会返回结果,但它会为每 100 个寄存器创建一个数组。我编辑答案以显示我在控制台上得到了什么
猜你喜欢
  • 2021-12-10
  • 2021-02-12
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
  • 2019-01-27
  • 2018-03-16
相关资源
最近更新 更多