【发布时间】:2019-09-26 21:50:23
【问题描述】:
我正在尝试将以下功能部署到 firebase。该函数部署良好,但当函数触发时出现错误:cannot read property 'parent' of undefined。错误发生在我引用父级的第一行。我在快照和快照.ref上使用了console.log,虽然快照存在,但snapshot.ref是未定义的。
我在其他云功能中使用了 snapshot.ref.parent,它工作正常。此功能有两个主要区别: (a) 它是一个 onUpdate(我之前一直在使用 onCreate 和 onDelete) (b) 它是一个异步函数。
exports.likeRating = functions.database.ref('Ocean/{waveId}/Likes').onUpdate(async (snapshot) =>{
let likes; let dislikes; let comments; let echoes;
await snapshot.ref.parent.child('Dislikes').once('value').then(response=>{dislikes = response.val(); return null});
await snapshot.ref.parent.child('Likes').once('value').then(response=>{likes = response.val(); return null});
await snapshot.ref.parent.child('Comments').child('CommentsCount').once('value').then(response=>{comments = response.val(); return null});
await snapshot.ref.parent.child('Echoes').once('value').then(response=>{echoes = response.val(); return null});
snapshot.ref.parent.child('Rating').set(dislikes+likes+comments+echoes);
return null;
}
关于我为什么会收到此错误的任何想法?感谢所有帮助。
【问题讨论】:
标签: javascript node.js firebase