0

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;
 });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
zafer
  • 407
  • 1
  • 4
  • 16
  • why do you keep uploading images to prnt.sc? It's prone to link rot and prevent an image to be inlined – phuclv Mar 25 '22 at 06:39

1 Answers1

3

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

Nader
  • 190
  • 9