如何在 onRequest 函数中使用 getFirestore() 正确编写云函数 v9?

how write correctly a cloud functions v9 with getFirestore() inside an onRequest function?

exports.testGetTransactions = functions.region('southamerica-east1').runWith({
  timeoutSeconds: 60,
  memory: '8GB',
}).https.onRequest((request, response) => {
  cors(request, response, () => {
    functions.logger.log('body', request)
    functions.logger.log('request body', request.body)
    let array = []
    getFirestore().collection('transactions').where('purchaseEpoch', '>', 1639842778924).where('purchaseEpoch', '<', 1642434778924).orderBy('purchaseEpoch', 'desc').limit(4000).get().then((querySnapshot) => {
      functions.logger.log('array', querySnapshot)
      querySnapshot.forEach((item, i) => {
        array.push(item.data())
      })
      functions.logger.log('array final', array)
      return response.status(200).send(array)
    }).catch((error) => {
      return response.status(400).send(error)
    })
  })
})

这个函数返回一个空数组,而这个函数在正常的客户端调用中得到近 300 个文件。

我做错了什么?我需要使用不同格式的查询/where 吗?

当 运行 在 Cloud Functions 等 Firebase 服务中时,V9 SDK 是 better used from clients such as mobile and web applications. Since you are building Cloud Functions that will interact with Firestore, you should instead use the Admin SDK. This SDK can be easily initialized

您只需对代码进行最少的更改,因为 Firestore 实例支持您一直使用的语法:

const admin = require('firebase-admin');
admin.initializeApp();

const db = admin.firestore(); // Firestore instance from admin SDK

exports.writeToFirestore = functions.firestore
  .document('some/doc')
  .onWrite((change, context) => {
    db.doc('some/otherdoc').set({ ... }); //Using the Firestore instance to update a document
  });

您还可以查看 API 参考以了解相关方法,例如 where() and relevant examples. For a complete sample, you can check out the code inside the quickstart repo