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

时间:2017-12-24 05:30:59

标签: firebase google-cloud-functions google-cloud-firestore

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

  1. 用户集合。
  2. Persons_id是自动生成的文档ID。
  3. Friends是一个包含persons_id的对象数组。
  4. 名称有第一个和最后一个对象。
  5. 我正在使用文档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",
    

1 个答案:

答案 0 :(得分:0)

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

Users (collection)
"c7xXQC5cEq5JyWyED8rX" (document)
Friends (collection)
"FadcjWG0GpDgkFujGWaa" (document)
person_id: string -> Users -> document id
confirmed: boolean