Firestore array

编程入门 行业动态 更新时间:2024-10-26 03:33:55
本文介绍了Firestore array-contains-any 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..
"@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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 11:31:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023071.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Firestore   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!