javascript - 为什么在chrome扩展中使用where子句时firestore不返回对象?
问题描述
我正在尝试使用 chrome 扩展从 Firestore 中过滤一些数据。我的第一个代码块有效并给出了 ch_id,但第二个(使用 where 子句过滤)没有返回对象。
第一个代码块。这可以正常工作并将消息 doc.data().ch_id 作为“ch001”。
svrGetVids("ch001");
function svrGetVids(ch_id)
{
try{
var docRef = db.collection("channel")
.doc("000000000000000000000001");
docRef.get().then(function(doc) {
if (doc.exists) {
alert( doc.data().ch_id);
} else {
alert('no doc found');
}
}).catch(function(error) {
alert(error.message);
});
}catch(error)
{
alert(error.message);
}
/*second code block. This does not return the object, and alerts 'no doc found'*/
try{
db
.collection("channel")
.where("ch_id",'==', ch_id)
.get()
.then(function(doc) {
if (doc.exists) {
alert( doc.data().ch_id);
} else {
alert('no doc found');
}
}).catch(function(error) {
alert('e1'+error.message);
});
}catch(error)
{
alert('e2'+error.message);
}
}
谁能在第二个代码块中找到我做错了什么?
解决方案
原因是因为你得到的对象的类型不同。return docRef.get()
a DocumentSnapshot
whiledb.collection("channel").where("ch_id",'==', ch_id).get()
返回 a QuerySnapshot
,它没有 exists 属性,如您在文档中所见。
因此,如果您想要相同的结果,则必须执行以下操作:
try{
db.collection("channel")
.where("ch_id",'==', ch_id)
.get()
.then(function(querySnapshot) {
if (!querySnapshot.empty) {
querySnapshot.foreach(function(doc) {
alert(doc.data().ch_id);
});
} else {
alert('no doc found');
}
}).catch(function(error) {
alert('e1'+error.message);
});
}catch(error)
{
alert('e2'+error.message);
}
推荐阅读
- node.js - 批量插入的 I/O 操作计数是多少?(在 MongoDB 上)
- reactjs - 如何使用 Jest/Enzyme 测试子组件中的回调函数 prop
- python - Python 数据类映射到 SQL Alchemy
- drone - 如何在区域设置无人机实例上登录
- python - Python,站点包部分位于网络文件系统上?
- c# - 使用 RegionInfo 将国家名称转换为自己的 ThreeLetterISORegionName (c# 2013 Winforms)
- wordpress - 如何修复“Woocommerce Cart PDF”功能文件以将 pdf 链接移动到结帐页面而不是购物车页面
- php - PHP - PHPMailer 使用多个 SMTP 主机发送电子邮件
- django - 未找到“about_me”的反向。“about_me”不是有效的视图函数或模式名称
- ios - 使用苹果配置器 2 安装应用程序 - 无法安装错误