亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何檢查firestore文檔中是否存在字段?

如何檢查firestore文檔中是否存在字段?

Helenr 2022-12-22 12:03:47
即使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);      });}
查看完整描述

1 回答

?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

你應該添加一個檢查,看看整個結果集中是否沒有文檔,這是一個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 => { ... })


查看完整回答
反對 回復 2022-12-22
  • 1 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號