问题描述
我正在使用Google Cloud功能更新Rooms / {pushId} / ins,该功能从几个门/ {MACaddress} / ins中获取新的 In 数据。
目前的功能如下:
exports.updateRoomIns = functions.database.ref('/ doors / {MACaddress}')。onWrite((change,context)=> {
const beforeData = change.before.val(); //写入前的数据(所有门子节点的数据)
const afterData = change.after.val(); //写入后的数据(所有门子节点的数据)
const roomPushKey = afterData.inRoom; //得到合适的房间
const insbefore = beforeData.ins;
const insafter = afterData.ins; //仅获取ins节点的后数据
console.log(insafter);
if( insbefore!= insafter){
const updates = {};
直接位于 const updates = {}创建一个空对象(稍后)填充,并(稍后)更新rooms / {roompushkey} / ins节点...
代码继续......
updates ['/ rooms /'+ roomPushKey +'/ ins'] = insafter; //用/ doors / {MACaddress / ins
return admin.database()。ref()。update(updates);中的值填充INS对象。 //更新
} else {
返回
}
});
如果我只有一扇门,这会有效,但由于我有几扇门有不同的Ins数据,每当我更新单个Door / {MACaddress / Ins时,整个Rooms / {pushId} / Ins会被替换为上次更新的门上的任何数据...我知道更新方法应该用于此目的,我有点想保留这个更新对象,以便稍后将数据扇出到其他路径。这可能吗?关于如何解决这个问题的任何建议?
这是我的数据结构:
root:{
门:{
111111111111:{
MAC地址:111111111111,
inRoom: - LBMH_8KHf_N9CvLqhzU,//我将需要此值clone的路径
ins:{
//我在这里创建了几个key:pair,类似于:
1525104151100:true,
1525104151183:true,
}
},
222222222222:{
MAC地址:222222222222,
inRoom: - LBMH_8KHf_N9CvLqhzU,//我将需要克隆路径的这个值
ins:{
//我在这里创建了几个key:pair,类似于:
2525104157710:true,
2525104157711:true,
}
}
},
房间:{
-LBMH_8KHf_N9CvLqhzU:{
ins:{
//我希望函数克隆相同的数据:
15251041511 00:true,
1525104151183:true,
}
}
}
从我之前对你的相关问题的回答中阐述(),我会调整代码如下:
exports.updateRoom = functions.database.ref('/ doors / {MACaddress}')。onWrite((change,context)=> {
const afterData = change.after.val(); //写入后的数据
const roomPushKey = afterData.inRoom;
const ins = afterData.ins;
const updates = {};
Object.keys(ins).forEach(key => {
updates ['/ rooms /'+ roomPushKey +'/ ins /'+ key] = true;
});
返回admin.database()。ref()。update(updates);
})。catch(错误=> {
console.log(错误);
// +其他必要的rerror处理
} );
换句话说,而不是替换完整的 ins
object,在现有的ins节点中添加新的子节点。
I am updating "Rooms/{pushId}/ins" with a Google Cloud function that gets new In data from several "doors/{MACaddress}/ins".The function currently goes like like this:
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 = {};
The line directly above "const updates = {}" creates an empty object to be (later) populated, and to (later) update the "rooms/{roompushkey}/ins" node...
I think the problem may be here, on the "updates" const, as this object is redefined as a whole every time the function runs...
the code proceeds...
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
}
});
This would work if I only had one door, but as I have several doors with different Ins data, every time I update a single "Door/{MACaddress/Ins", the whole "Rooms/{pushId}/Ins" gets replaced for whatever data is on the last updated door... I know the update method should be used for this purpose, and I kinda want to keep this "updates" object to fan out the data to other paths later. Is this possible? Any suggestions on how to solve this?
This is my data structure:
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,
}
}
}
Elaborating from my previous answer to your related question (How to clone a node to another path based on a reference value from the initial path on Google Cloud Functions?), I would adapt the code as follow:
exports.updateRoom = functions.database.ref('/doors/{MACaddress}').onWrite((change, context) => {
const afterData = change.after.val(); // data after the write
const roomPushKey = afterData.inRoom;
const ins = afterData.ins;
const updates = {};
Object.keys(ins).forEach(key => {
updates['/rooms/' + roomPushKey + '/ins/' + key] = true;
});
return admin.database().ref().update(updates);
}).catch(error => {
console.log(error);
//+ other rerror treatment if necessary
});
In other words, instead of replacing the full ins
object, you add new children nodes in the existing ins node.
这篇关于如何使用更新对象更新节点而不使用Google Cloud Function覆盖所有子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!