一个网页调用一个firebase函数,该函数应该从firestore集合中获取多条记录,所有内容都会正常运行,但不会返回任何数据。当我直接在页面中运行查询时(不是通过函数),将返回正确的数据。
在网页中:
var getUserConfigFiles = firebase.functions().httpsCallable('getUserConfigFiles');
getUserConfigFiles()
.then((result) => {
console.log("Yay - Firebase result ===>",result);
})
.catch((error) => {
console.warn("error",error.code,error.message,error.details)
});
在index.js中:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const cors = require('cors')({origin: true});
const db = admin.firestore();
然后函数本身:
exports.getUserConfigFiles = functions.https.onCall((data, context) => {
if (!context.auth) {
return {"status": "error", "code": 499, "message": "The function must be called while authenticated"};
}
const uid = context.auth.uid;
const email = context.auth.token.email;
var outcome = {"status": "OK", "code": 200, "requestor": uid, "email": email, "configfiles":[]};
// search on either the user's userid or their email
outcome.searcharray = [uid];
if (email) {
outcome.searcharray.push(email);
}
return db.collection("configfiles").where("memberAttached", "array-contains-any", outcome.searcharray)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
outcome.configfiles.push({"tokenid": doc.id, "data-all": doc.data()});
})
// next row is returning the object just to see if it's there
outcome.querySnapshot = querySnapshot;
//
return (outcome);
})
.catch((error) => {
return ({"status": "error", "code": 402, "message": error,"requestor": uid});
});
});
一切正常,结果返回querysnapshot。除了,没有数据,.configfiles应该有大约一百行。如果我运行 db.collection("configfiles").where("memberAttached...
在web页面中,返回数据。
我已经寻找并尝试了很多方法,但我显然遗漏了一些基本的东西。有人能帮忙吗?
1条答案
按热度按时间lfapxunr1#
我怀疑
outcome.querySnapshot = querySnapshot
线路出现问题,如querySnapshot
不是json对象,因此无法返回。我建议删除该行,然后重试。如果这还不能解决问题,您可以添加一些日志记录,看看代码是否到达
then
?