Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Firebase Cloud Function not working as expected

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading