【问题标题】:Firestore with promises and push带有承诺和推送的 Firestore
【发布时间】:2017-12-30 22:43:05
【问题描述】:

感谢 Firebase 的 Frank 提供的helping me with this code。我只是在将文档 ID 推送到 Friends 集合下时遇到了这个问题。我不确定在下面的代码中将const friendIdconst accepted 推送到friendsList 数组的最佳方法是什么。

const db = admin.firestore();
const friendRef = 
db.collection('users').doc(id).collection('friends');

friendRef.get().then((onSnapshot) => {
  var promises = [];

  onSnapshot.forEach((friend) => {
    const personId = String(friend.data().person_id);
    const friendId = String(friend.id);
    const accepted = friend.data().accepted;

    promises.push(db.collection('users').doc(personId).get());
  });

  Promise.all(promises).then((snapshots) => {
    friendsList = [];
    snapshots.forEach((result) => {
      friendsList.push({
        friendId: friendId,
        accepted: accepted,
        firstName: result.data().name.first,
        lastName: result.data().name.last,
      });
    });
    res.send(friendsList);
  });
}).catch((e) => {
  res.send({
    'error': e
  });
})

我尝试了一些方法,但没有成功。任何帮助将不胜感激。

【问题讨论】:

  • 究竟是什么不起作用?你在哪里定义friendsList 数组?在您编写friendId: friendId 的代码部分中,变量friendId 未定义。他们留在onSnapshot.forEach的块中

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


【解决方案1】:

问题是您将每个friend 值从调用db.collection('users').doc(personId).get() 中获得的内容推入promises 数组。而且你永远不会为每个朋友保留personIdfriendIdaccepted 的值,除非作为局部变量。

您应该将它们保留在每个承诺数组中。为此,您可以像这样返回自定义 Promise。

promises.push(new Promise((resolve, reject) => {
    db.collection('users').doc(personId).get()
    .then(docResult => {
        resolve({         
            friendId: friendId,
            accepted: accepted,
            doc: docResult
        });
    })
    .catch(reason => {
        reject(reason);
    });
});

然后当你迭代快照数组时:

snapshots.forEach((result) => {
    friendsList.push({
        friendId: result.friendId,
        accepted: result.accepted,
        firstName: result.doc.data().name.first,
        lastName: result.doc.data().name.last,
    });
});

【讨论】:

    【解决方案2】:

    正如我刚刚在对您最初问题的回答中所评论的那样,我的代码中有一个错字,原因是变量名称令人困惑。

    const db = admin.firestore();
    const friendRef = db.collection('users').doc(id).collection('friends');
    
    friendRef.get().then((friendsSnapshot) => {
      var promises = [];
    
      friendsSnapshot.forEach((friend) => {
        const friendId = String(friend.data().person_id);    
        promises.push(db.collection('users').doc(friendId).get());
      });
    
      Promise.all(promises).then((friendDocs) => {
        friendsList = [];
        friendDocs.forEach((friendDoc) => {
          friendsList.push({
            personId: friendDoc.id,
            firstName: friendDoc.data().name.first,
            lastName: friendDoc.data().name.last,
          });
        });
        res.send(friendsList);
      });
    }).catch((e) => {
      res.send({
        'error': e
      });
    })
    

    由于您使用doc(friendId) 查找朋友的数据,因此then() 回调中的friendDoc.id 将是每个朋友的ID。

    在这种情况下,我强烈建议将 Cloud Firestore 参考文档放在手边。例如,在这种情况下,我找到了DocumentSnapshot.id 来做需要做的事情。

    【讨论】:

    • 谢谢弗兰克,但我接受了@TurkhanBadalov 的回答,因为它为我提供了一种将const accepted 推送到friendsList 的方法。
    猜你喜欢
    • 1970-01-01
    • 2017-02-28
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2018-05-09
    相关资源
    最近更新 更多