这是我第一次使用where().get()
进行Firestore查询,但无法正常使用。我的Firestore数据:
此代码有效:
firebase.firestore().collection('Tunes').doc('tiny_dancer')
.get()
.then(function(querySnapshot) {
if (querySnapshot.exists) {
console.log(querySnapshot.data());
} else {
console.log("No such document!");
}
})
.catch(function(error) {
console.error(error);
});
我们回到{composer: "elton_john"}
。
此代码无效:
firebase.firestore().collection('Tunes')
.where('composer', '==', 'elton_john')
.get()
.then(function(querySnapshot) {
if (querySnapshot.exists) {
console.log(querySnapshot.data());
} else {
console.log("No such document!");
}
})
.catch(function(error) {
console.error(error);
});
响应为No such document!
,我期望它返回tiny_dancer
。我该怎么办?
答案 0 :(得分:2)
有一个DocumentSnapshot.exists
属性,可让您检查文档是否实际存在。但是对于查询来说,可以有任意数量的匹配文档,因此,您可以使用QuerySnapshot.empty
来代替exists
来检查是否存在任何匹配项。