【发布时间】:2023-02-09 20:34:34
【问题描述】:
我是 Firestore 函数的新手,我开始将它们与实时数据库结合使用。我想要实现的目标 - 如果 ' 下的任何字段分数' 集合发生变化(在本例中为 't1' 或 't2'),则 'round' 字段应递增 1。文档如下所示:
这是我的功能:
exports.onScoreChange = functions.database
.ref('games/{gameId}/scores')
.onWrite((change, context) => {
console.log('Score of either player has changed');
var round = change.before.ref.parent('gameInfo/round').get('round') + 1;
console.log('Rounds so far: ' + round);
return change.ref.parent('gameInfo/round').set(round);
});
当我查看日志时,该函数被触发,因为我可以在日志中看到消息“任一玩家的分数已更改”,但随后我收到此错误:
change.before.ref.parent is not a function
我的逻辑流程是,我应该在文档上方执行一个“步骤”,这样我就可以访问属于游戏“gameInfo”集合的其他字段 - 然后我可以访问字段“round”并更改它。脚本有什么问题?
【问题讨论】:
标签: javascript firebase-realtime-database google-cloud-functions