且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在“数组包含"查询不适用于引用的Firestore

更新时间:2023-02-05 15:38:18

@Alex Mamo答案是正确的,您需要一个DocumentReference.

@Alex Mamo answer is right, you need a DocumentReference.

注意:您不能自己创建一个!

您必须通过查询firebase来获得参考!

You have to get the reference by querying firebase!

代码:

return this.auth.currUser
      .pipe(
        take(1),
        switchMap(user => this.afs
            .collection('chats', ref => ref.where('members', 'array-contains', this.afs
              .collection('users')
              .doc<User>(user.uid)
              .ref))
            .snapshotChanges()
            .pipe(
              map(actions => {
                return actions.map(action => {
                  const data = action.payload.doc.data() as Chat;
                  const id = action.payload.doc.id;
                  return {id, ...data};
                });
              })
            ) as Observable<Chat[]>
        )
      );

关键部分是价值"部分,即:

The crucial part is the 'value' part which is:

this.afs
              .collection('users')
              .doc<User>(user.uid)
              .ref

您查询并通过获得参考.

You query and THEN get the reference with .ref!

就是这样!这就是查询DocumentReference的方式!

That's it! That's how you query for a DocumentReference!