Javascript Firestore子集合查询在生产中不起作用
下面的代码在我们的开发firebase项目中按预期运行,但在中不会返回任何结果 我们的生产项目。这两个项目的功能、触发器、文档结构和索引似乎是相同的。我们一定错过了一些小东西 正在加载“module2Attempts”子集合中所有现有文档的问题的firestore查询如下:Javascript Firestore子集合查询在生产中不起作用,javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,下面的代码在我们的开发firebase项目中按预期运行,但在中不会返回任何结果 我们的生产项目。这两个项目的功能、触发器、文档结构和索引似乎是相同的。我们一定错过了一些小东西 正在加载“module2Attempts”子集合中所有现有文档的问题的firestore查询如下: admin.firestore().collection("users").doc("jcGP0aE2RSf0f0vmR15LGa6QNIu1").collection("module2Attempts") .get
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);
});
}
我们已确认data.uid不为null或未定义,并且在查询定义的收集路径上确实存在多个文档
函数/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);
onCreateModule2trunt.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
未返回在所有工作完成时解析的承诺。这是一个云计算功能,因此它知道何时可以安全地清理您的工作。如果它有时起作用,而其他的不起作用,那么你所观察到的是一个种族状况
至少,你应该做的是:
return admin.firestore().collection("users").doc().collection().get()...
请注意返回语句。我猜您的安全规则就是问题所在。您是否有错误消息?不幸的是,函数日志中没有。我认为这也是罪魁祸首,但我们做了一个差异检查,比较了开发安全规则和prods规则,它们是匹配的。你的代码根本没有检查错误。你应该在promise上使用catch()来确定get是否失败。我可以在客户端应用程序上运行相同的查询,而不会出现任何问题。我也很抱歉它被删除以缩短post的代码…我确实有一个未被触发的catch…catch(函数(错误){console.error(“错误加载模块2尝试文档:”,错误); });