【问题标题】:How do i query a collection with the new change in @angular/fire version 7.2.0?如何使用 @angular/fire 7.2.0 版中的新更改查询集合?
【发布时间】:2022-01-01 21:29:53
【问题描述】:

我想通过仅过滤掉具有特定 ID 的文档来查询项目集合。

import { Firestore, collectionData, collection } from '@angular/fire/firestore';
import { Observable } from 'rxjs';

interface Item {
  name: string,
  ...
};

@Component({
  selector: 'app-root',
  template: `
  <ul>
    <li *ngFor="let item of item$ | async">
      {{ item.name }}
    </li>
  </ul>
  `
})
export class AppComponent {
  item$: Observable<Item[]>;
  constructor(firestore: Firestore) {
    const collection = collection(firestore, 'items');
    this.item$ = collectionData(collection);
  }
}

【问题讨论】:

  • 你能提供一个我们可以复制的示例代码吗?我发现这个Github link 可能对你有帮助。
  • 你能做到吗?
  • @AlbertoEspinoza 请检查我刚刚发布的答案,希望它有助于解决您的问题。

标签: javascript angular google-cloud-firestore


【解决方案1】:

我设法按照以下步骤进行查询

// Import statement
import { Firestore, collectionData, collection,where,query } from '@angular/fire/firestore';

// Please define an Observable like below
Items: Observable<Item[]>; // Item being a model

// inside your function
const appRef = collection(this.firestore, 'items')
const appQuery = query(appRef, where('uid', '==', userId));

this.Items= (await collectionData(appQuery)).pipe(
    map((actions) => {
      return actions.map((a) => {
        const data = a as Item;
        const uid = a.id;
        return { uid, ...data };
      });
    })
  );

【讨论】:

    猜你喜欢
    • 2019-09-18
    • 1970-01-01
    • 2021-06-07
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    相关资源
    最近更新 更多