4
votes

I have a data structure made this way:

  • Posts(collection)
  • UserId(document)
  • Posts(collection)
  • postDoc (document)

And I'm setting a cloud function to change all Posts(subcollection) documents upon a certain event, and in order to do so I'm using collectionGroup queries:

This is how I set it up:

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

In the Firebase Log I receive the following error:

TypeError: querySnapshot.forEach is not a function

Searching online I found out that there may be a problem with Firebase SDK version but mine is uptodate, I attach the package.json file here:

{
  "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
}
1
How do you define db?Renaud Tarnec
const db = admin.firestore(); @RenaudTarnecStackGU

1 Answers

3
votes

The get() method is asynchronous, so you either need to use then() to get the querySnapshot when the Promise returned by get() is fulfilled, or use async/await. More details on how to deal with asynchronous calls in this SO answer.

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

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

Note the return null; at the end. See this doc item for more details on this key point.


Note also that if you want to update several docs within the forEach() loop, you will need to use Promise.all(). Many SO answers cover this case.