【发布时间】:2017-04-02 04:25:01
【问题描述】:
我正在监控节点叶作业/{jobid}/proposals 的变化。每当我删除提案时,该函数就会执行并重新插入提案(这是预期的行为)。 问题是当我删除其父 {job} 时,提案会重新插入具有相同父 ID 的新对象中。有没有办法检查父母是否存在?如果是,请重新插入提案,否则不要。
exports.RecountProposals = functions.database.ref("/jobs/{jobid}/proposals").onWrite(event => {
const jobid = event.params.jobid;
if (!event.data.exists() && event.data.ref.parent.exists()) {
const propRef = admin.database().ref(`proposals/${jobid}`);
const counterRef = event.data.ref;
const collectionRef = counterRef.parent.child('proposals');
// Return the promise from counterRef.set() so our function
// waits for this async event to complete before it exits.
return propRef.once('value')
.then(messagesData => collectionRef.set(messagesData.numChildren()));
}
});
我正在检查父级是否存在,但显示错误:
event.data.ref.parent.exists()
TypeError: event.data.ref.parent.exists 不是函数
【问题讨论】:
标签: firebase firebase-realtime-database google-cloud-functions