Firebase 函数无法访问 Firestore 数据



我已经编写并部署了一个Firebase函数。该函数应该从用户那里获取一个值,一个orgKey,如果它等于firestore中的orgKey,则该函数返回附加到该orgKey的userType。我知道函数从客户端接收输入,但它总是返回null,无论我是否发送了一个应该有效的orgKey。有人能说出哪里出了问题吗?

index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.authOrgKey = functions.https.onCall((data, context) => {
db = admin.firestore();
functions.logger.log('data passed to function: ', data);
db.collection("Orgkeys")
.get()
.then(snapshot => {
snapshot.forEach(doc => {
if (doc.orgKey == data) {return doc.userType}
});
return false;
});
});

现在您只从回调中返回值,但这些值不会到达调用者。您需要从代码的顶层返回一个值。你可以用来增加你现在的回报值

exports.authOrgKey = functions.https.onCall((data, context) => {
db = admin.firestore();
functions.logger.log('data passed to function: ', data);
return db.collection("Orgkeys")
.get()
.then(snapshot => {
snapshot.forEach(doc => {
if (doc.orgKey == data) {return doc.userType}
});
return false;
});
});

不相关:考虑使用查询来确定与orgKey匹配的文档,以避免必须读取集合中的所有文档才能找到其中一个。

最新更新