узел.
аюсь обновить (добавить) некоторых детей в базе данных Firebase. В этом случае мне нужно обновить узел, не перезаписывая все дочерние узлы с помощью Google Cloud Function, используя объект обновления.
Этот вопрос вытекает из этого здесь:Как обновить узел, не перезаписывая все дочерние узлы с помощью Google Cloud Function, используя объект обновления?
Моя структура данных выглядит так:
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,
}
}
}
Я пытаюсь запросить существующие «модули» и перебрать объект, чтобы определить, существует ли он, прежде чем я добавлю его, чтобы я мог добавить суффикс к ключу на случай, если он уже существует. Это моя функция съела момент.
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: Невозможно прочитать свойство 'on' из неопределенного в Object.keys.forEach.key
Я уверен, что я слишком усложняю, но я не смог найти более чистую логику для этого.
Есть ли у вас какие-либо предложения о том, как сделать это осторожно? Является ли это возможным?