I'm trying to remove the event node once it expires and remove all it's children image of the event node before getting removed
the problem is when the time passes and i delete the event node all it's children get removed except this child
image of the event node after getting removed
source code
exports.removeOldEvents = functions.https.onRequest((req, res) => {
const eventsRef = admin.database().ref('events')
eventsRef.once('value', (snapshot) => {
snapshot.forEach((child) => {
child.forEach((child) => {
if (1000*Number(child.val()['endDate']) <= new Date().getTime()) {
child.ref.set(null)
}
})
})
})
return res.status(200).end()
})
Since you call several time the set()
method, which returns a promise, you should use Promise.all()
in order to wait all the promises resolve before sending back the response.
The following adaptation of your code should work (not tested however):
exports.removeOldEvents = functions.https.onRequest((req, res) => {
const eventsRef = admin.database().ref('events')
eventsRef.once('value')
.then((snapshot) => {
const promises = [];
snapshot.forEach((child) => {
child.forEach((child) => {
if (1000*Number(child.val().endDate) <= new Date().getTime()) {
promises.push(child.ref.set(null));
}
});
});
return Promise.all(promises);
})
.then(results => {
const responseObj = {response: 'success'};
res.send(responseObj);
})
.catch(err => {
res.status(500).send(err);
})
});
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.