gpt4 book ai didi

javascript - Firebase Firestore where() 查询找不到文档

转载 作者:行者123 更新时间:2023-12-01 01:24:18 26 4
gpt4 key购买 nike

这是我第一次使用 where().get() 进行 Firestore 查询,但我无法让它工作。我的 Firestore 数据:

enter image description here

此代码有效:

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

响应是没有这样的文档!我期望它返回tiny_dancer。我该怎么做?

最佳答案

有一个 DocumentSnapshot.exists 属性,可让您检查文档是否确实存在。但对于查询来说,可以有任意数量的匹配文档,因此您可以使用 QuerySnapshot.empty 而不是 exists检查是否有任何匹配。

关于javascript - Firebase Firestore where() 查询找不到文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53949816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com