创建新节点时,我想创建相同的数据并将其推送到不同的节点。
Door / 111111111111 / ins 节点是我将新数据推送到的节点:
root: {
doors: {
111111111111: {
MACaddress: "111111111111",
inRoom: "-LBMH_8KHf_N9CvLqhzU",
ins: {
// I am creating several "key: pair"s here, something like:
1525104151100: true,
1525104151183: true,
}
}
},
rooms: {
-LBMH_8KHf_N9CvLqhzU: {
ins: {
// I want the function to clone the same data here:
1525104151100: true,
1525104151183: true,
}
}
}
这是我的函数,它抛出一个错误:
TypeError:change.before.ref.parent.child(...)。val不是函数
代码:
exports.updateRoom = functions.database.ref('/doors/{MACaddress}/ins').onWrite((change, context) => {
const beforeData = change.before.val(); // data before the write
console.log(beforeData); // all good so far
const afterData = change.after.val(); // data after the write
console.log(afterData); // all good so far
const roomPushKey = change.before.ref.parent.child('/inRoom').val(); // ERROR
console.log(roomPushKey);
return change.after.ref.parent.parent.parent.child('/rooms').child(roomPushKey).child('/ins').set(afterData);
});
我的道路有什么问题?如何让它更新其他节点?