Reputation: 83
I am making an app and having an issue in deleting firebase DB using cloud function on specific date.
This is how I am storing data , I want to delete this Node on nextDonationDate , I search a lot and found this can be done with cloud function, but I did not know how to do and access this because dates are saving under UserID of user so facing problem to make ref for that and how can I solve my problem to perform execution on nextDonationDate? Thanks :)
Upvotes: 1
Views: 1449
Reputation: 5841
may be the cloud function code will be something like this :
exports.deleteByDate = functions.https.onRequest((req, res) => {
//url example
// deletebydate?date=Oct 28, 2017
var targetDate = req.query.date;
var db = admin.database();
var donorDbRef = db.ref('Blood_donors_last_donation_date');
var updateVal = {};
donorDbRef
.orderByChild('nextDonationDate')
.equalTo(targetDate)
.once('value')
.then( snapshot => {
snapshot.forEach(childSnapshot => {
updateVal[childSnapshot.key] = null
});
donorDbRef.update(updateVal, (error)=>{
if(!error)
res.status(200).send('succeed deleting data!');
else
res.status(200).send('failed deleting data!');
});
});
});
Upvotes: 2
Reputation: 5984
Firebase has a tutorial on Youtube where they showed how to schedule a Cron Job for a cloud function trigger.
https://www.youtube.com/watch?v=CbE2PzvAMxA
You can have this Cron running at regular intervals which will perform an HTTP call to your function. Here you can loop through all the nodes and checking the nextDonationDate as per your logic and delete the nodes which match your criteria.
You can optimize as per your requirement, this is a rough way to do this as i don't exactly know what and why you need to do something.
Upvotes: 1