简体   繁体   English

对firestore文档的角度firestore查询

[英]Angular firestore query to firestore document

I have below query我有以下查询

    selectedUser$: AngularFirestoreDocument<any>;
    this.selectedUser$ = this.userCollection.ref.where('uid', '==', key)

Throwing error投掷错误

Type 'Query' is not assignable to type 'AngularFirestoreDocument'.类型“查询”不可分配给类型“AngularFirestoreDocument”。 Property 'ref' is missing in type 'Query'. “查询”类型中缺少属性“参考”。

I tried我试过了

this.selectedUser$ = this.userCollection.ref.where('uid', '==', key).get()

no success没有成功

Basically, I want the query to return firestore document基本上,我希望查询返回 firestore 文档

The error your getting is because you're mixing the firebase native api & angularfire.您得到的错误是因为您正在混合使用 firebase 本机 api 和 angularfire。

selectedUser$: AngularFirestoreDocument<any>;

Calling .ref on your AngularFirestoreCollection converts it to type firebase.firestore.CollectionReference.ref上调用AngularFirestoreCollection会将其转换为firebase.firestore.CollectionReference类型

That being said, there are 2 possibilities to solve your issue:话虽如此,有两种可能性可以解决您的问题:

Using angularfire :使用angularfire

I'm assuming your userCollection looks something like this: this.afs.collection<User> .我假设您的 userCollection 看起来像这样: this.afs.collection<User> Since you're querying a collection, there is no way to ensure your query predicate uid == key is unique in firebase.由于您正在查询集合,因此无法确保您的查询谓词uid == key在 firebase 中是唯一的。 So you query the collection and limit() the result.因此,您查询集合并limit()结果。 This will return you an array with one document.这将返回一个包含一个文档的数组。 flatMap() will return you a single user. flatMap()将返回一个用户。

this.afs.collection<User>('users', ref => ref.where('uid', '==', key).limit(1))
   .valueChanges()
   .pipe(
       flatMap(users=> users)
   );

Using the firebase native api:使用firebase原生 api:

const query = this.usersCollection.ref.where('uid', '==', key);
query.get().then(querySnapshot => {
    if (querySnapshot.empty) {
        console.log('no data found');
    } else if (querySnapshot.size > 1) {
        console.log('no unique data');
    } else {
        querySnapshot.forEach(documentSnapshot => {
            this.selectedUser$ = this.afs.doc(documentSnapshot.ref);
            // this.afs.doc(documentSnapshot.ref).valueChanges().subscribe(console.log);
            });
        }
    });

This way it's a bit easier to chain multiple .where clauses if it's needed这样,如果需要,链接多个.where子句会更容易一些

This worked for me with the latest versions: 2021-July这适用于我的最新版本:2021-7 月

this.store
    .collection("Products",ref=>ref.where("stock","==",10))
    .get()
    .subscribe(data=>data.forEach(el=>console.log(el.data())));

PS: I'm using get() and no event listener. PS:我正在使用 get() 并且没有事件侦听器。

this.afs.collection<User>('users', ref => ref.where('uid', '==', key).limit(1))
      );

It will be easier to help you if you add more details about the structure of your Firestore collection and your typescript file.如果您添加有关 Firestore 集合结构和 typescript 文件的更多详细信息,将会更容易为您提供帮助。

However, to query the collection I do the following:但是,要查询集合,我执行以下操作:

  1. Define a private AngularFirestore in the constructor.在构造函数中定义一个私有的 AngularFirestore。

     constructor( private afStore: AngularFirestore, ) { }
  2. Define the query and pass the result to the AngularFirestoreDocument.定义查询并将结果传递给 AngularFirestoreDocument。

     this.selectedUser$ = this.afStore .collection('TheNameOfYourCollection').doc<any>(key);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM