简体   繁体   中英

Firestore Sub Collection Query Not Working in Production

The code below runs as expected in our development firebase project but will not return any results in our production project. The functions, triggers, document structures, indexes appear to be identical for both projects. We must be missing something small.

The firestore query that is having the issue loading all existing documents within the "module2Attempts" subcollection is here:

admin.firestore().collection("users").doc("jcGP0aE2RSf0f0vmR15LGa6QNIu1").collection("module2Attempts")
    .get()
    .then(function (docs) {
      if (docs.empty) {
        console.log("No docs found from query.");
        return [];
      }
      let prevAttempts = [];
      docs.docs.forEach(function (doc) {
        // doc.data() is never undefined for query doc snapshots
        prevAttempts.push(doc.data());
      });
      console.log(prevAttempts.length+" attempts found!");
      return prevAttempts;
    })
    .catch(function (error) {
      console.error("Error loading module 2 attempt docs: ", error);
    });
}

We have confirmed data.uid is not null or undefined and multiple documents do exist at the collection path defined by the query.

functions/index.js

'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');

const prodServiceAccount = require("./prodServiceAccountKey.json");
const devServiceAccount = require("./devServiceAccountKey.json");

const config = process.env.REACT_APP_STAGE === "prod" ?
  {
    credential: admin.credential.cert(prodServiceAccount),
    databaseURL: "https://...."
  } :
  {
    credential: admin.credential.cert(devServiceAccount),
    databaseURL: "https://..."
  };

admin.initializeApp(config);

const onCreateModule2Attempt = require('./Modules/onCreateModule2Attempt.js');


// Listen for new module2Attempt docs created
module.exports.createModule2Attempt = functions.firestore
  .document('users/{userId}/module2Attempts/{attemptId}')
  .onCreate(onCreateModule2Attempt.handler);

onCreateModule2Attempt.js

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


exports.handler = async (snap, context) => {
  const data = snap.data();
  console.log("This log is just a test balloon")
  console.log("New Module 2 Attempt for ", data.email);

  admin.firestore().collection("users").doc("jcGP0aE2RSf0f0vmR15LGa6QNIu1").collection("module2Attempts")
    .get()
    .then(function (docs) {
      if (docs.empty) {
        console.log("No docs found from query.");
        return [];
      }
      let prevAttempts = [];
      docs.docs.forEach(function (doc) {
        // doc.data() is never undefined for query doc snapshots
        prevAttempts.push(doc.data());
      });
      console.log(prevAttempts.length+" attempts found!");
      return prevAttempts;
    })
    .catch(function (error) {
      console.error("Error loading module 2 attempt docs: ", error);
    });
}

exports.handler isn't returning a promise that resolves when all the work is complete. This is a requirement by Cloud Functions, so it knows when it's safe to clean up your work. If it works sometimes and not others, then what you're observing is a race condition.

Minimally, what you should be doing is this:

return admin.firestore().collection("users").doc().collection().get()...

Note the return statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM