![](/img/trans.png)
[英]How to check whether a document field, of type array, already contains a value through Firestore rules?
[英]How to check whether field exists or not in the firestore document?
即使if(doc.exists)
工作正常,但如果我單擊的特定字母沒有數據,它不會顯示控制台消息 - console.log("There is a no song which starting with clickedletter");
例如 - 如果我點擊按鈕“A”並成像有一首以字母“A”開頭的歌曲。 此數據快速彈出,但是當我單擊 firestore 文檔中不存在數據的按鈕“T”時,未顯示控制台消息。
function getID(a){
console.log("youclicked", a.id);
var id = a.id;
//getting this "a" value from an alphabetic pagination bar----------
const clickedletter = a.getAttribute('value');
var Aristid = sessionStorage.getItem("getArtistID");
console.log("current artist ID "+ searchforid);
db.collectionGroup('MP3_Songs').where("Idofartist", "==", Aristid).orderBy("Song_name").startAt(clickedletter).endAt(clickedletter +'\uf88ff').get().then((documentSnapshot) =>{
documentSnapshot.forEach(doc=> {
if(doc.exists){
const data = doc.data();
console.log("start with clickedletter", data);
gotdata.innerText = "Song name: " + data.Song_name;
} else{
// this is the problem I got. This console log does not show if there is no data.---
console.log("There is a no song which starting with clickedletter");
}
})
}).catch(function(error) {
console.log("Error getting document:", error);
});
}
您應該添加一個檢查以查看整個結果集中是否沒有文檔,這是一個QuerySnapshot類型的對象。 它有一個名為empty的屬性。
db.collectionGroup('MP3_Songs').where("Idofartist", "==", Aristid).orderBy("Song_name").startAt(clickedletter).endAt(clickedletter +'\uf88ff').get()
.then((querySnapshot) => {
if (querySnapshot.empty) {
// here, there are no documents in the query results
}
else {
// here, you can iterate the documents to find matches
querySnapshot.forEach(documentSnapshot => { ... })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.