Home > OS >  Firebase Cloud Function not working as expected
Firebase Cloud Function not working as expected

Time:08-04

exports.resetDailyFinalKills = functions.pubsub
    .schedule("58 16 * * *")
    .onRun(async (context) => {
      const players = firestore.collection("players");
      const goodtimes = await players.where("final_kills", ">", 0);
      goodtimes.forEach((snapshot) => {
        snapshot.ref.update({final_kills: 0});
      });
      return null;
    });

So I have this cloud function, and when I force run it nothing happens at all like it just says the function was successful but the final_kills field never gets updated. Can anyone help? Firestore Like I obviously have a player here which has a final_kills value that is greater than 0. So why doesn't this reset that back down to zero?

CodePudding user response:

Note sure if I am missing something here but:

You actually try to iterate over the Query object firebase creates when using the where() function on your collections. You actually never fetch the data from the database.

const players = firestore.collection("players");
// fetch the objects from firestore
const goodtimes = await players.where("final_kills", ">", 0).get();

// iterate over the docs you receive
goodtimes.docs.forEach((snapshot) => {
    snapshot.ref.update({ final_kills: 0 });
});

Edit (regarding your comment): Make sure you set your timezone properly after your .schedule() function:

// timezone in my case
functions.pubsub.schedule('5 11 * * *').timeZone('Europe/Berlin')

Check this list as a reference for your correct timezone.

  • Related