【发布时间】: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