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

标签 javascript node.js firebase google-cloud-firestore google-cloud-functions

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

  • 帖子(收藏)
  • 用户ID(文档)
  • 帖子(收藏)
  • 博士后(文档)

  • 我正在设置一个云功能来更改某个事件的所有 Posts(subcollection) 文档,为此我使用了 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 Log我收到以下错误:
    TypeError: querySnapshot.forEach is not a function
    
    搜索 online我发现 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()方法是异步 , 所以你要么需要使用 then()获取 querySnapshotget() 返回的 Promise已完成,或使用 async/await .关于如何处理异步调用的更多细节在这个 SO answer .
    then()

    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;
      })
    });
    
    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(async (context) => {
      const querySnapshot = await db.collectionGroup("Posts").where("isVisible", "==", true).get();
      querySnapshot.forEach((doc) => {
         console.log(doc.data());
      });
      return null;
    });
    

    注意 return null;在最后。看到这个doc item有关此关键点的更多详细信息。

    另请注意,如果您想更新 forEach() 中的多个文档循环,您将需要使用 Promise.all() .许多SO答案都涵盖了这种情况。

    关于javascript - TypeError : querySnapshot. forEach 不是函数 - Firebase 云函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66015207/

    相关文章:

    node.js - Node - 用于测试的 stub Nodemailer 传输

    javascript - Node + Express session 过期?

    ios - 在从函数返回之前等待 Firebase 加载

    ios - 应用程序在后台时未收到 Swift FCM 数据消息 (iOS 10)

    javascript - 将图像覆盖在 div 中心水平和中心垂直在 div 上

    javascript - Shiny and rhandsontable - 基于列总和的条件单元格/列格式

    css - 如何扩展 sass/compass 库

    java - 我可以使用 if else 语句编写 Firestore 查询吗?

    javascript - 将一组未删节的连续字符串值解析为可用的可索引键值对

    javascript - JsFiddle 上的鼠标事件不起作用?