gpt4 book ai didi

javascript - 如何使用 Google Cloud Function 查询现有的子对象并循环对象以检测它是否存在于 Firebase 中?

转载 作者:行者123 更新时间:2023-12-03 01:57:53 24 4
gpt4 key购买 nike

我正在尝试更新(添加)Firebase 数据库上的一些子项。在这种情况下,我需要使用更新对象更新节点,而不用 Google Cloud Function 覆盖所有子节点。

This question derives from this one here: How to update a node without overwriting all the child nodes with Google Cloud Function using a update Object?

我的数据结构是这样的:

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,
}
}
}

我试图查询现有的“ins”并循环对象以在添加它们之前检测它是否存在,以便我可以在键中添加后缀,以防它们已经存在。这是我的函数吃的时刻。

 exports.updateBuildingsOuts = 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 = {};
// forEach loop to update the keys individually
Object.keys(ins).forEach(key => {
parentPath = ['/rooms/' + roomPushKey + '/ins/']; // defining the path where I want to check if the data exists
// querying the "ins"
admin.database().ref().parentPath.on('value', function(snapshot) {
if (snapshot.hasChild(key)) {
updates['/rooms/' + roomPushKey + '/ins/' + key + "_a"] = true; // define 'updates' Object adding a suffix "_a"

return admin.database().ref().update(updates); // do the update adding the suffix "_a"

} else {
updates['/rooms/' + roomPushKey + '/ins/' + key] = true; // define update Object without suffix

return admin.database().ref().update(updates); // do the 'updates' without suffix
}
});
});

此云函数正在检索错误:

TypeError: Cannot read property 'on' of undefined at Object.keys.forEach.key

我确信我过于复杂化了,但我无法为此找到更清晰的逻辑。

您对如何精益地做到这一点有什么建议吗?可能吗?

最佳答案

您可以执行以下操作:

let afterIns;
exports.updateRoom = functions.database.ref('/doors/{MACaddress}').onWrite((change, context) => {

const beforeData = change.before.val(); //data before
const insBefore = beforeData.ins;

const afterData = change.after.val(); // data after the write
const roomPushKey = afterData.inRoom;
afterIns = afterData.ins;

return admin.database().ref().update(updates);

const updates = {};

Object.keys(ins).forEach(key => {
if (insBefore.hasOwnProperty(key)) { // checking if the insBefore object has a key equal to value 'key'
//True -> add an extra character at the existing key
updates['/rooms/' + roomPushKey + '/ins/' + key + '_a'] = true; //<- _a being the extra character
} else {
updates['/rooms/' + roomPushKey + '/ins/' + key] = true;
}
});

return admin.database().ref().update(updates);

}).catch(error => {
console.log(error);
//+ other error treatment if necessary

});

不要忘记该函数返回 change.before.val(); 之前的数据(以及之后的数据),因此您不必执行 admin。 database().ref().parentPath.on('value', function(snapshot) {

<小时/>

评论后编辑

这应该可行,但我还没有测试过。除了测试它是否为 null 之外,您可能还需要测试 insBefore 是否未定义。我让你微调它。

let insAfter;
let roomPushKey ;
exports.updateRoomIns = functions.database.ref('/doors/{MACaddress}').onWrite((change, context) => {
const afterData = change.after.val(); // data after the write
roomPushKey = afterData.inRoom;
insAfter = afterData.ins;
return admin.database().ref('/rooms/' + roomPushKey).once('value').then(snapshot => {
const insBefore = snapshot.val().ins;
const updates = {};
if (insBefore === null || insBefore === undefined ) {
Object.keys(insAfter).forEach(key => {
updates['/rooms/' + roomPushKey + '/ins/' + key] = true;
});
} else {
Object.keys(insAfter).forEach(key => {
if (insBefore.hasOwnProperty(key)) {
updates['/rooms/' + roomPushKey + '/ins/' + key + '_a'] = true;
} else {
updates['/rooms/' + roomPushKey + '/ins/' + key] = true;
}
});
}
return admin.database().ref().update(updates);
});
});.catch(error => {
console.log(error);
//+ other error treatment if necessary

});

关于javascript - 如何使用 Google Cloud Function 查询现有的子对象并循环对象以检测它是否存在于 Firebase 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50156736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com