Search code examples
javascriptfirebasegoogle-cloud-firestorecrongoogle-cloud-functions

Not able to read data from firestore using schedule functions


There are multiple documents in collection 'Test1'. I want to read the documents of this collection which are the objects. I could se "completeOrder?? QuerySnapshot {" in log message but not the objects.

const db = admin.firestore();
exports.indianTimeCrontab = functions.pubsub
  .schedule('1 16 * * *')
  .timeZone('America/Los_Angeles') // Users can choose timezone - default is America/Los_Angeles
  .onRun(async () => {
    const completeOrder = await db.collection('Test1').get();
    console.log('completeOrder??', completeOrder);
    return null;
  });

Solution

  • By using the get() method with await db.collection('Test1').get(); you actually get a QuerySnapshot.

    You then need to use the forEach() method or the docs property of the QuerySnapshot in order to get the documents (i.e. the "objects").

    For example, you can use forEach() as follows:

      exports.indianTimeCrontab = functions.pubsub
        .schedule('1 16 * * *')
        .timeZone('America/Los_Angeles') // Users can choose timezone - default is America/Los_Angeles
        .onRun(async () => {
          const querySnapshot = await db.collection('Test1').get();
         
          querySnapshot.forEach((doc) => {
            console.log(doc.id, ' => ', doc.data());
          });
          return null;
        });