Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 具有firestore的云函数找不到任何数据_Javascript_Firebase_Firebase Authentication_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Javascript 具有firestore的云函数找不到任何数据

Javascript 具有firestore的云函数找不到任何数据,javascript,firebase,firebase-authentication,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Firebase Authentication,Google Cloud Firestore,Google Cloud Functions,我不熟悉js和firestore(以及整个firebase生态系统) 我想用两个where字段(帐户id、设备id)查询数据,但我总是得到“找不到文档” 我甚至尝试删除where子句,但是仍然找不到数据,但数据仍然存在: 上下文和数据中的参数是 //UID=UIDwFK2JVghw8XjVGqlEE0Uj09irGK2 //设备ID=552cbe50f935de7a 根据要求,以下是完整代码: exports.authDevice = functions.https.onCall((data,

我不熟悉js和firestore(以及整个firebase生态系统)

我想用两个where字段(帐户id、设备id)查询数据,但我总是得到“找不到文档”

我甚至尝试删除where子句,但是仍然找不到数据,但数据仍然存在:

上下文和数据中的参数是 //UID=UIDwFK2JVghw8XjVGqlEE0Uj09irGK2 //设备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);
    });
});

context.auth
仅适用于实时数据库触发函数。从:

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

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


context.auth
仅适用于实时数据库触发函数。从:

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

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


经过一些调试,我发现它不会返回单个文档,而是返回一个“查询快照”,该快照的方法为空或大小为

更改后:

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

经过一些调试,我发现它不会返回单个文档,而是返回一个“查询快照”,该快照的方法为空或大小为

更改后:

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

我认为应该将调用链接在一起:
让selectQuery=admin.firestore().collection(“设备”).where(“account\u id”,“context.auth.uid”).where(“device\u id”,“deviceId”,“deviceId”)-但我仍然希望它能按原样显示数据,只是不进行任何过滤。根据章节:无效:不同字段上的范围过滤器显示这是不应该使用的方式良好的捕获,它们确实需要链接在一起:
admin.firestore().collection(“devices”).where(“account\u id”,“=”,context.auth.uid)。where(“device_id”,“==”,deviceId).get()。然后(函数(doc){…
@user93466:但您没有范围筛选器。您有两个相等筛选器。请看复合查询的第一个示例:
citiesRef.where('state','=','CO')。where('name','==','Denver'))
我认为您应该将调用链接在一起:
让selectQuery=admin.firestore().collection(“设备”).where(“account\u id”,“==”,context.auth.uid)。where(“device\u id”,“==”,deviceId)
-但我仍然希望它能按原样显示数据,只是不进行任何过滤。根据章节:无效:不同字段上的范围过滤器显示这是不应该使用的方式。好的捕获,它们确实需要链接在一起:
admin.firestore().collection(“设备”).where(“account_id”,“=”,context.auth.uid)。where(“device_id“,”==”,deviceId).get().then(函数(doc){…
@user93466:但您没有范围筛选器。您有两个相等筛选器。请看复合查询的第一个示例:
citiesRef.where('state','=','CO')。where('name','==','Denver');
嘿,context.auth不是空的/未定义的-所有数据都在那里(我做了一个console.log),一切都是它应该做的,而且这不是实时数据库触发器,而是onCall触发器-请更新您的问题以包含该信息。嘿,context.auth不是空的/未定义的-所有数据都在那里(我做了一个console.log)一切都是它应该是的方式,而且这不是实时数据库触发器,而是onCall触发器-请更新您的问题,以包括该信息。