简体   繁体   English

如何检查firestore文档中是否存在字段?

[英]How to check whether field exists or not in the firestore document?

Even though if(doc.exists) works perfectly, But if there is no data on a specific letter which i clicked, It does not show the console message - console.log("There is a no song which starting with clickedletter");即使if(doc.exists)工作正常,但如果我单击的特定字母没有数据,它不会显示控制台消息 - console.log("There is a no song which starting with clickedletter");

For example - If I clicked button "A" and imaging there is a song which starting letter "A".例如 - 如果我点击按钮“A”并成像有一首以字母“A”开头的歌曲。 This data pop up quickly, But When I clicked button "T" which Data does not exist in the firestore document is not showing console message.此数据快速弹出,但是当我单击 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);
      });

}

You should add a check to see if there are no documents in the entire result set, which is a QuerySnapshot type object.您应该添加一个检查以查看整个结果集中是否没有文档,这是一个QuerySnapshot类型的对象。 It has a property called empty .它有一个名为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.

相关问题 如何通过 Firestore 规则检查数组类型的文档字段是否已包含值? - How to check whether a document field, of type array, already contains a value through Firestore rules? 如何检查Firestore文档是否具有特定字段 - How to check if firestore document have a particular field 使用实时更新时如何检查云 Firestore 文档是否存在 - How to check if a cloud firestore document exists when using realtime updates 检查 Firebase Firestore 中是否存在文档,如果不存在则新建一个 - Check if a document exists in Firebase Firestore, if not create a new Firestore - 如何根据特定字段检查文档是否存在 - Firestore - How to check if a document exist based on a specific field Firestore expressjs:如何获取特定字段存在的文档并更新文档 - Firestore expressjs: how to get document where specific field exists and update the document 如何检查文档/文档快照中是否存在字段? - How can you check if a field exists in a document/documentSnapshot or not? 无法检查云 Firestore 数据库中是否存在文档以及 reactjs - Can't check if document exists in cloud firestore database and with reactjs 如何检查JavaScript中是否存在对象密钥? - How to check whether an object key exists or not in JavaScript? 如何从javascript检查会话是否存在? - How to check whether session exists from javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM