Firestore array-contains-any is not working properly

こ雲淡風輕ζ 提交于 2020-01-22 02:30:08

问题


"@angular/fire": "5.2.3", 
"firebase": "7.4.0",

Note: members is an array and 0,1,2 is map data structures.

service.ts

 getUserChatGroups(uid: string): Observable<DocumentChangeAction<MessageGroupMemberModel>[]> {
    return this.afs.collection<MessageGroupMemberModel>(`messageGroups`, ref => ref.where('members', 'array-contains-any', [uid])).snapshotChanges();
  }

page.ts

 init(): void {
    const uid: string = this.authService.getUserUid();
    this.chatService.getUserChatGroups(uid).subscribe(res => {
      console.log(res);
    },
      err => console.log(err),
      () => console.log('request completed')
    );

  }

No errors. But it doesn't return any value. But it has values. I hope something wrong with my Query. Any help here?


回答1:


The array-contains-any (and also the array-contains) operator check whether the array contains an element that example matches the information that you pass into the call. So in your case, you need to pass in both the id and the joinDateTime in order to match.

ref.where('members', 'array-contains-any', [{ id: uid, joinedDateTime: ... }])

If you don't know the joinedDateTime for the user, you can't query the array in your current data structure. The normal solution is to store a separate array field memberIds in the document too, which contains just the UIDs of the members. Then you can find the members by querying on that field:

ref.where('members', 'array-contains', [uid])

Or

ref.where('members', 'array-contains-any', [uid, uid2, uid3])


来源:https://stackoverflow.com/questions/59052349/firestore-array-contains-any-is-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!