I'm using Firebase Cloud Functions to delete a child node when the event is expired and all the values get removed except a nested child node it doesn't get removed
what's the issue and how to fix it?
exports.removeOldMessages = functions.https.onRequest((req, res) => {
const messagesRef = admin.database().ref('events')
messagesRef.once('value', (snapshot) => {
snapshot.forEach((child) => {
child.forEach((child) => {
if (Number(child.val()['endDate']) <= new Date().getTime()) {
child.ref.set(null)
}
})
})
})
return res.status(200).end()
})
here is the JSON
{ "events" : { "N5iTuYzAbJa02RauxCl3uh2Nggz1" : {
"-LNmIvSdrwK96KCGcmXm" : {
"addedBy" : "Riyadh Figures",
"coordinate" : [ 24.70914690943994, 46.78851541131735 ],
"endDate" : "1538442801.0",
"imagePath" : "-LNmIvSdrwK96KCGcmXm",
"key" : "-LNmIvSdrwK96KCGcmXm",
"title" : "hjihgf",
"userPicture" : "N5iTuYzAbJa02RauxCl3uh2Nggz1" } }
I don't understand what you're trying to achieve, by far you want to delete the event node if it's expired. Try it like below
var deleteObj ={}
messagesRef.once('value', (snapshot) => {
snapshot.forEach((childSnap) => {
childSnap.forEach((innerChild) => {
if (Number(innerChild.val()['endDate']) <= new Date().getTime()) {
// you can do this in two ways
// step 1: Direct Deleting
admin.database().ref('/events/' + childSnap.key + '/' + innerChild.key).set({});
// step 2: Pass into Object
deleteObj['/events/' + childSnap.key + '/' + innerChild.key] = null;
// As you asked for deleting coordinate
deleteObj['/events/' + childSnap.key + '/' + innerChild.key +'/' + 'coordinate'] = [] or {}; // this will do
}
})
})
})
if(deleteObj) // Step 2
admin.database().ref().update(deleteObj).then(function(){
// console.log("Deleted");
})
Step 2 will be helpful if you have more than one nodes to delete as fb support multipath update or delete
Keep one thing, as Doug said in your question comment. Why you have download entire data to delete some nodes alone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.