问题描述
限时送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
)操作符检查数组是否存在包含一个元素,该元素与您传递给调用的信息相匹配.因此,在您的情况下,您需要同时传入 id
和 joinDateTime
以进行匹配.
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:关键词]
发布评论