【问题标题】:Firebase Function can't acces Firestore dataFirebase 函数无法访问 Firestore 数据
【发布时间】:2021-01-03 16:25:22
【问题描述】:

我已经编写并部署了一个 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;
    });
});

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    现在您只能从回调中返回值,但这些值不会到达调用者。您需要从代码的顶层返回一个值。您可以通过以下方式冒泡您现在拥有的返回值:

    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;
            });
    });
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 2020-02-01
      • 2019-02-01
      • 2017-07-27
      相关资源
      最近更新 更多