I’m dealing with a reservation system.ı use firebase realtime database. So I want to remove past data (historically, before today). I decided to use firebase functions for this.I try to use if else (I don’t know if there is an easier way) but I couldn’t use break and return commands. How can I stop this after deleting the days before today.
database structure : https://prnt.sc/AyubX0SQG3oo
exports.removePrev = functions.pubsub.schedule("every 5 minutes").onRun((context) => { const day = new Date(); const targetDateFormat = new Date(day).toLocaleDateString("en-US", { year: "numeric", month: "2-digit", day: "2-digit"}); const resultDay = targetDateFormat.replace(/[/]/g, "-"); admin.database().ref("reservations/").once("value").then((snap)=>{ snap.forEach((days)=>{ if (days.key != resultDay) { admin.database().ref("reservations/"+days.key).remove(); } else if (days.key==resultDay) { //Want to break; } }); }); return null; });
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
forEach
does not support break
, check this answer
Short circuit Array.forEach like calling break
alternatively you can use for of
, MDN Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for…of
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0