为什么在 chrome 扩展中使用 where 子句时 firestore 不 return 对象?
Why firestore does not return object when using where clause in chrome extension?
我正在尝试使用 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);
}
}
谁能找出我在第二个代码块中做错了什么?
原因是你获取的对象类型不同。 docRef.get()
returns 一个 DocumentSnapshot
while db.collection("channel").where("ch_id",'==', ch_id).get()
returns a QuerySnapshot
,它不存在 属性,正如您在文档中看到的那样。
所以如果你想要相同的结果,你将不得不这样做:
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);
}
我正在尝试使用 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);
}
}
谁能找出我在第二个代码块中做错了什么?
原因是你获取的对象类型不同。 docRef.get()
returns 一个 DocumentSnapshot
while db.collection("channel").where("ch_id",'==', ch_id).get()
returns a QuerySnapshot
,它不存在 属性,正如您在文档中看到的那样。
所以如果你想要相同的结果,你将不得不这样做:
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);
}