【发布时间】:2018-10-12 20:07:33
【问题描述】:
我正在使用 Google Cloud 函数更新“Rooms/{pushId}/ins”,该函数从多个“门/{MACaddress}/ins”获取新的 In 数据。 目前的功能是这样的:
exports.updateRoomIns = functions.database.ref('/doors/{MACaddress}').onWrite((change, context) => {
const beforeData = change.before.val(); // data before the write (data of all the doors child nodes)
const afterData = change.after.val(); // data after the write (data of all the doors child nodes)
const roomPushKey = afterData.inRoom; // get the right room
const insbefore = beforeData.ins;
const insafter = afterData.ins; // get the after data of only the "ins" node
console.log(insafter);
if (insbefore != insafter) {
const updates = {};
“const updates = {}”正上方的行创建一个空对象(稍后)填充,并(稍后)更新“rooms/{roompushkey}/ins”节点...
我认为问题可能出在“更新”常量上,因为每次函数运行时都会将这个对象重新定义为一个整体......
代码继续...
updates['/rooms/' + roomPushKey + '/ins'] = insafter; // populate the "INS" object with the values taken from the "/doors/{MACaddress/ins"
return admin.database().ref().update(updates); // do the update
} else {
return
}
});
如果我只有一扇门,这会起作用,但由于我有几扇门具有不同的 Ins 数据,所以每次我更新单个“Door/{MACaddress/Ins”时,整个“Rooms/{pushId}/Ins”被最后更新的门上的任何数据替换...我知道 update 方法应该用于此目的,我有点想保留这个“更新”对象以将数据散开到以后的其他路径。这可能吗?有关如何解决此问题的任何建议?
这是我的数据结构:
root: {
doors: {
111111111111: {
MACaddress: "111111111111",
inRoom: "-LBMH_8KHf_N9CvLqhzU", // I will need this value for the clone's path
ins: {
// I am creating several "key: pair"s here, something like:
1525104151100: true,
1525104151183: true,
}
},
222222222222: {
MACaddress: "222222222222",
inRoom: "-LBMH_8KHf_N9CvLqhzU", // I will need this value for the clone's path
ins: {
// I am creating several "key: pair"s here, something like:
2525104157710: true,
2525104157711: true,
}
}
},
rooms: {
-LBMH_8KHf_N9CvLqhzU: {
ins: {
// I want the function to clone the same data here:
1525104151100: true,
1525104151183: true,
}
}
}
【问题讨论】:
标签: javascript firebase firebase-realtime-database google-cloud-functions