【发布时间】:2021-07-27 01:49:53
【问题描述】:
网页调用 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});
});
});
一切正常,结果返回一个查询快照。除了没有数据,.configfiles 应该有一百左右的行。如果我只在网页中运行db.collection("configfiles").where("memberAttached... 部分,则会返回数据。
我已经搜索并尝试了许多方法,但我显然缺少一些基本的东西。有人可以帮忙吗?
【问题讨论】:
标签: javascript firebase google-cloud-firestore google-cloud-functions