简体   繁体   English

Firestore 子集合查询在生产中不起作用

[英]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.下面的代码在我们的开发 firebase 项目中按预期运行,但不会在我们的生产项目中返回任何结果。 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:在“module2Attempts”子集合中加载所有现有文档时出现问题的 firestore 查询在这里:

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.我们已经确认 data.uid 不为空或未定义,并且在查询定义的集合路径中确实存在多个文档。

functions/index.js函数/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 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. exports.handler没有返回在所有工作完成后解决的承诺。 This is a requirement by Cloud Functions, so it knows when it's safe to clean up your work.这是 Cloud Functions 的一项 要求,因此它知道何时可以安全地清理您的工作。 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.注意返回语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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