【问题标题】:Firestore array-contains-any is not working properlyFirestore array-contains-any 无法正常工作
【发布时间】:2019-11-26 13:44:53
【问题描述】:
"@angular/fire": "5.2.3", 
"firebase": "7.4.0",

注意:membersarray,0,1,2 是 map 数据结构。

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')
    );

  }

没有错误。但它不返回任何值。但它有价值观。我的查询有问题吗?

【问题讨论】:

    标签: javascript google-cloud-firestore angularfire


    【解决方案1】:

    array-contains-any(以及array-contains)运算符检查数组是否包含与您传递给调用的信息相匹配的元素。因此,在您的情况下,您需要同时传递 idjoinDateTime 才能匹配。

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

    如果您不知道用户的joinedDateTime,则无法查询当前数据结构中的数组。正常的解决方案是在文档中也存储一个单独的数组字段memberIds,其中仅包含成员的 UID。然后你可以通过查询 that 字段找到成员:

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

    或者

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

    【讨论】:

    猜你喜欢
    • 2020-04-04
    • 1970-01-01
    • 2019-03-13
    • 2020-07-24
    • 2020-11-01
    • 2021-07-19
    • 1970-01-01
    • 2020-11-29
    • 2021-12-12
    相关资源
    最近更新 更多