0
votes

I am trying to get all of the documents in one collection named 'repeated_tasks' in my Firebase Function.

I tried using the following code: Is it possible to get all documents in a Firestore Cloud Function?, but this does not seem to work for me. I am trying to get the information, so that I can update every document in the collection, to set one field, to false. I have the following code:

exports.finishedUpdate = functions.pubsub.schedule('0 3 * * *').timeZone('Europe/Amsterdam').onRun((context) => {

//  This is part of the above mentioned stack question
    var citiesRef = database.collection('repeated_tasks');
    const snapshot = citiesRef.get();
    snapshot.forEach(doc => {
      console.log(doc.id, '=>', doc.data());
    });

// A way to update all of the documents in the repeated_tasks collection has to be found

//  This part works, for only the two given document ids
    var list = ['qfrxHTZAJZTJDQpA83fjsM03159438695', 'qfrxHTZAJZQTpM3pA83fjsM0315217389'];

    for (var i = 0; i < list.length; i++) {
        database.doc('repeated_tasks/' + list[i]).update({'finished': false});
    }

    return console.log("Done");
})

Help is much appreciated, as I can't seem to find any related information anywhere, except for the stack overflow page, which didn't help. I am using Node JS (Javascript) to set the functions.

1

1 Answers

2
votes

By using the syntax of getting the information from Firestore, I was able to also update it in Firebase Functions and could update all, using the following code:

    const reference = database.collection('repeated_tasks/');
    const snapshot = await reference.where('finished', '==', true).get();
    if (snapshot.empty) {
        console.log('no matching documents');
        return;
    }

    snapshot.forEach(doc => {
        database.doc('repeated_tasks/' + doc.id).update({'finished': false});
    });