无法使用调度函数从 firestore 读取数据

Not able to read data from firestore using schedule functions

集合中有多个文档 'Test1'。我想阅读这个集合的文件,这些文件是对象。我可以在日志消息中看到“completeOrder?? QuerySnapshot {”而不是对象。

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;
  });

通过使用 get() method with await db.collection('Test1').get(); you actually get a QuerySnapshot.

然后您需要使用 QuerySnapshotforEach() method or the docs 属性 来获取文档(即“对象”)。

例如,您可以按如下方式使用forEach()

  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;
    });