从函数中的firebase firestore中检索朋友的名字

问题描述 投票:0回答:1

在Firebase函数中检索朋友的名字和姓氏的最佳方法是什么?我尝试了几种方法,但它没有用。

  1. 用户集合。
  2. 人员ID是自动生成的文档ID。
  3. Friends是一个包含persons_id的对象数组。
  4. 名称具有第一个和最后一个对象。

我正在使用文档ID发出http请求,并使用我朋友的名/姓的数组进行响应。

const id = String(req.body.id);
admin.firestore().collection('users').doc(id);

friends = [];
friends.push(jsonObject); // not sure if i should do a for loop
res.send({ friends: friends }); // responding with array

公司的FireStore:

Users (collection)
"c7xXQC5cEq5JyWyED8rX" (document)
name: {
  first: "John",
  last: "Doe",
},
friends: [ {
  persons_id: "0fLXpei6kEgrCkfFCeJi",
  confirmed: true,
},
  persons_id: "65ghQC5cEq5JyWyED8rX",
  confirmed: false,
} ],
username: "johndoe",
firebase google-cloud-functions google-cloud-firestore
1个回答
0
投票

看起来对我来说最好的解决方案是与朋友创建一个子集合,并使用person_id,确认等填充它。用户集合仍然会有名字,姓氏等。

Users (collection)
"c7xXQC5cEq5JyWyED8rX" (document)
Friends (collection)
"FadcjWG0GpDgkFujGWaa" (document)
person_id: string -> Users -> document id
confirmed: boolean
© www.soinside.com 2019 - 2024. All rights reserved.