Javascript Firebase功能可以';t访问Firestore数据
我已经编写并部署了Firebase函数。该函数应该从用户获取一个值,即orgKey,如果它等于firestore中的orgKey,则该函数返回附加到该orgKey的userType。我知道该函数接收来自客户端的输入,但它总是返回null,无论我发送的orgKey是否正常工作。 有人能告诉我是怎么回事吗 index.jsJavascript Firebase功能可以';t访问Firestore数据,javascript,node.js,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,我已经编写并部署了Firebase函数。该函数应该从用户获取一个值,即orgKey,如果它等于firestore中的orgKey,则该函数返回附加到该orgKey的userType。我知道该函数接收来自客户端的输入,但它总是返回null,无论我发送的orgKey是否正常工作。 有人能告诉我是怎么回事吗 index.js const functions = require('firebase-functions'); const admin = require('firebase-admin');
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/代码>匹配的文档,以防止必须从集合中读取所有文档以找到其中的一个。