英文:
Query to fetch friend matching a document reference from firestore
问题
以下是翻译好的部分:
这是我拥有的用户数据
->firstName
->lastName
->uid
->grade
->friends[<Document Reference UID>] 例如:[users/1, users/2, 等等]
现在我想要获取 uid 为 '1' 的存在的用户列表
String userid="1";
const userRef = admin.firestore().collection('users').doc(userid).get();
var friends = await users
.where('friends', 'array-contains', userRef)
.limit(3)
.get();
英文:
Here is the Users data that I have
->firstName
->lastName
->uid
->grade
->friends[<Document Reference UID>] eg: [users/1, users/2, etc]
Now I want to fetch a list of uid where uid '1' exists
String userid="1";
const userRef = admin.firestore().collection('users').doc(userid).get();
var friends = await users
.where('friends', 'array-contains', userRef)
.limit(3)
.get();
答案1
得分: 0
这行代码:
```javascript
const userRef = admin.firestore().collection('users').doc(userid).get();
您的userRef
值不是DocumentReference
,而实际上是Promise<DocumentSnapshot>
。这在后续的.where('friends', 'array-contains', userRef)
条件中不会起作用 - 因为不会有任何匹配该类型的文档。
您想要的是:
const userRef = admin.firestore().collection('users').doc(userid);
所以不要在最后加上.get()
。现在userRef
包含一个DocumentReference
,它将匹配具有相同文档引用的文档中的文档。
<details>
<summary>英文:</summary>
This line:
const userRef = admin.firestore().collection('users').doc(userid).get();
Your `userRef` value is not a `DocumentReference`, but is actually a `Promise<DocumentSnapshot>`. And that won't work in the subsequent `.where('friends', 'array-contains', userRef)` condition - as there won't be any documents matching that type.
---
What you want is:
const userRef = admin.firestore().collection('users').doc(userid);
So without `.get()` at the end. Now `userRef` contains a `DocumentReference`, which will match documents that have that same document reference in the `friends` array.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论