Firestore array-contains-any 无法正常工作 [英] Firestore array-contains-any is not working properly

查看:25
本文介绍了Firestore array-contains-any 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

注意:members是一个array,0,1,2是map数据结构.

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. Is there something wrong with my query?

推荐答案

array-contains-any(还有array-contains)操作符检查数组是否存在包含一个元素,该元素与您传递给调用的信息相匹配.因此,在您的情况下,您需要同时传入 idjoinDateTime 以进行匹配.

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: ... }])

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

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])

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

这篇关于Firestore array-contains-any 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆