【问题标题】:Cloud functions with firestore cannot find any data带有firestore的云功能找不到任何数据
【发布时间】:2018-11-07 08:47:30
【问题描述】:

我是 js 和 firestore(以及整个 firebase 生态系统)的新手

我想用 2 个 where 字段(account_id、device_id)查询数据,但我总是得到“找不到文档”

let selectQuery = admin.firestore().collection("devices");
    selectQuery.where("account_id", "==", context.auth.uid);
    selectQuery.where("device_id", "==", deviceId);

    selectQuery.get().then(function(doc) {
        if (doc.exists) {
            console.log("Document data:", doc.data());
        } else {
            // doc.data() will be undefined in this case
            console.log("No such document!");
        }
        return "asd";
    }).catch(function(error) {
        console.log("Error getting document:", error);
    });

我什至尝试删除 where 子句,但仍然找不到数据,但它就在那里:

来自上下文和数据的参数是 // UID = UIDwFK2JVghw8XjVGqlEE0Uj09irGK2 // DEVICE_ID = 552cbe50f935de7a

这里的请求是完整的代码:

exports.authDevice = functions.https.onCall((data, context) => {
    if (!context.auth) {
        // Throwing an HttpsError so that the client gets the error details.
        throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
            'while authenticated.');
    }

    const deviceName = data.device_name;
    const deviceId = data.device_id;
    const isQR = data.is_qr;
    const uid = context.auth.uid;

    console.log("DEVICE NAME: " + deviceName);
    console.log("DEVICE ID: " + deviceId);
    console.log("is QR: " + isQR);
    console.log("UID: " + uid);

    admin.firestore().collection("devices")
        .where("account_id", "==", context.auth.uid)
        .where("device_id", "==", deviceId)
        .get().then(function(doc) {
        if (doc.exists) {
            console.log("Document data:", doc.data());
        } else {
            // doc.data() will be undefined in this case
            console.log("No such document!");
        }
        return "asd";
    }).catch(function(error) {
        console.log("Error getting document:", error);
    });
});

【问题讨论】:

  • 我认为您应该将调用链接在一起:let selectQuery = admin.firestore().collection("devices").where("account_id", "==", context.auth.uid).where("device_id", "==", deviceId); - 但我仍然希望它按原样显示数据,只是没有任何过滤。
  • 根据firebase.google.com/docs/firestore/query-data/queries部分:无效:不同字段上的范围过滤器表明这是不应该使用的方式
  • 很好,他们确实需要链接在一起:admin.firestore().collection("devices").where("account_id", "==", context.auth.uid).where("device_id", "==", deviceId).get().then(function(doc) {...
  • @user93466:但是你没有范围过滤器。你有两个平等过滤器。查看复合查询的第一个示例:citiesRef.where('state', '==', 'CO').where('name', '==', 'Denver');

标签: javascript firebase firebase-authentication google-cloud-firestore google-cloud-functions


【解决方案1】:

context.auth 仅适用于实时数据库触发函数。来自documentation for the EventContext.auth parameter

触发该功能的用户的身份验证信息。此对象包含经过身份验证的用户的 uid 和 token 属性。有关包括令牌密钥在内的更多详细信息,请参阅安全规则参考。

对于未经身份验证的用户,此字段为 null对于不提供用户信息的事件类型(实时数据库除外)或 Firebase 管理员用户,此字段将不存在。

【讨论】:

  • 嘿,context.auth 不是 null/undefined - 所有数据都在那里(我做了一个 console.log),一切都是它应该的样子
  • 这也不是实时数据库触发器,而是 onCall 触发器 - firebase.google.com/docs/functions/callable
  • 请更新您的问题以包含该信息。
【解决方案2】:

经过一些调试,我发现它没有返回单个文档,而是返回一个“QUERY SNAPSHOT”,其方法为空或大小。

改动后:

return admin.firestore().collection("devices").where("account_id", "==", context.auth.uid)
        .where("device_id", "==", deviceId).get().then((snapshot) => {
        snapshot.forEach((doc) => {
            console.log(doc.id, '=>', doc.data());
        });
        console.log("Empty: " + snapshot.empty);
        console.log("Size: " + snapshot.size);
        return "asd"
    })
        .catch((err) => {
            console.log('Error getting documents', err);
        });

【讨论】:

    猜你喜欢
    • 2019-08-10
    • 2021-03-27
    • 1970-01-01
    • 2021-11-18
    • 2021-05-06
    • 1970-01-01
    • 2020-08-02
    • 2019-08-26
    • 1970-01-01
    相关资源
    最近更新 更多