Javascript TypeError:querySnapshot.forEach不是函数-Firebase云函数

Javascript TypeError:querySnapshot.forEach不是函数-Firebase云函数,javascript,node.js,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,我有一个这样的数据结构: 员额(收集) 用户ID(文档) 员额(收集) 博士后(文件) 我正在设置一个云函数,以便在某个事件发生时更改所有POST(子集合)文档,为此,我使用collectionGroup查询: 我是这样设置的: exports.changeIsVisibleFieldAfterDay = functions.pubsub .schedule("every 2 minutes").onRun((context) => { const queryS

我有一个这样的数据结构:

  • 员额(收集)
  • 用户ID(文档)
  • 员额(收集)
  • 博士后(文件)
我正在设置一个云函数,以便在某个事件发生时更改所有POST(子集合)文档,为此,我使用
collectionGroup
查询:

我是这样设置的:

exports.changeIsVisibleFieldAfterDay = functions.pubsub
.schedule("every 2 minutes").onRun((context) => {
  const querySnapshot = db.collectionGroup("Posts")
      .where("isVisible", "==", true).get();
  querySnapshot.forEach((doc) => {
    console.log(doc.data());
  });
});
Firebase日志中
我收到以下错误:

TypeError: querySnapshot.forEach is not a function
搜索发现Firebase SDK版本可能有问题,但我的版本是最新版本,我将
package.json
文件附加到此处:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "12"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^9.2.0",
    "firebase-functions": "^3.11.0"
  },
  "devDependencies": {
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}
get()
方法是异步的,因此当
get()
返回的承诺实现时,您需要使用
then()
来获取
querySnapshot
,或者使用
async/wait
。更多关于如何在本教程中处理异步调用的详细信息

使用
then()
使用
async/await

注意
返回null在末尾。有关此关键点的更多详细信息,请参见此



还请注意,如果要更新
forEach()
循环中的多个文档,则需要使用。很多答案都涉及到这个问题。

如何定义
db
?const db=admin.firestore()@Renaudtarnec完美的回答,关于您在底部所做的小编辑,我还有一个问题,如果我想使用then()语句更新几个文档,我还需要使用Promise.all()吗?最后,
then()
选项和
async/await
选项是可以互换的,因此我可以自由地将其中一个选项用于另一个?是的,如果要并行执行对异步方法的多个调用(如
update()
),您需要使用
Promise.all()
)。您可以查看这个搜索,它列出了几个使用云函数的示例。如果你用更新的代码更新你的问题,我就可以看了
then()
选项和
async/await
选项可以互换,但不能混用。如果你认为我的回答帮助了你,请投票并接受它,谢谢。当然,我投票并接受了它,真的很有帮助,谢谢!
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

const db = admin.firestore();

exports.changeIsVisibleFieldAfterDay = functions.pubsub
.schedule("every 2 minutes").onRun((context) => {
  return db.collectionGroup("Posts").where("isVisible", "==", true).get()
  .then(querySnapshot => {
     querySnapshot.forEach((doc) => {
      console.log(doc.data());
     });
     return null;
  })
});
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

const db = admin.firestore();

exports.changeIsVisibleFieldAfterDay = functions.pubsub
.schedule("every 2 minutes").onRun(async (context) => {
  const querySnapshot = await db.collectionGroup("Posts").where("isVisible", "==", true).get();
  querySnapshot.forEach((doc) => {
     console.log(doc.data());
  });
  return null;
});