Firebase Firestore排行榜功能

时间:2018-10-30 18:58:22

标签: javascript node.js firebase google-cloud-functions

我正在尝试编写一个函数,该函数基于orderBy查询向每个用户添加排名,这应根据位置向每个孩子添加排名,然后应推送更新排行榜/节点和mobile_user节点排名

我遇到以下错误:

Argument "data" is not a valid Document. Couldn't serialize object of type "QueryDocumentSnapshot". Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the 'new' operator). 

这是我的代码的样子:

   exports.leaderboardUpdate2 = functions.https.onRequest((req, res) =>{
  const updates = [];
  const leaderboard = {};

  const rankref = admin.firestore().collection('mobile_user');
  const leaderboardRef = admin.firestore().collection('leaderboard');

  return rankref.orderBy("earned_points").limit(10).get().then(function(top10) {
      let i = 0;
      console.log(top10)
      top10.forEach(function(childSnapshot) {
        const r = dataSnapshot.size - i;
        console.log(childSnapshot)
        updates.push(childSnapshot.ref.update({rank: r}));
        leaderboard[childSnapshot.key] = Object.assign(childSnapshot, {rank: r});
        i++;
      });
// up to here everything seems to be working fine as it updates the childSnapshot with a rank,
      updates.push(leaderboardRef.set(leaderboard));
      return Promise.all(updates);
    }).then(() => {
      res.status(200).send("Mobile user ranks updated");
    }).catch((err) => {
      console.error(err);
      res.status(500).send("Error updating ranks.");
    });
});

我迷路了,我找不到关于该错误的任何信息,我将不胜感激

0 个答案:

没有答案