从Firestore获取与文档引用匹配的朋友查询。

huangapple go评论59阅读模式
英文:

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&lt;DocumentSnapshot&gt;`. And that won&#39;t work in the subsequent `.where(&#39;friends&#39;, &#39;array-contains&#39;, userRef)` condition - as there won&#39;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>



huangapple
  • 本文由 发表于 2023年6月15日 12:58:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定