简体   繁体   中英

How to retrieve data from document in Firestore db using HMTL/Javascript

This should be something really easy to do, but I am lost...

My data is structured: UserInfo / UID / Attribute / Value

I am able to pull the data for a specific UID, but I cannot retrieve the Value for a given Attribute (lastname in this case).

db.collection("UserInfo").doc("TEST").get().then(function(doc){
           if(doc.exists) {
               console.log("Document data: ", doc.data());
               //console.log("Lastname1: ", doc.lastname);
               //console.log("Lastname2: ", doc.lastname());
               //console.log("Lastname3: ", doc.lastname.val());
               //console.log("Lastname4: ", doc.lastname.val);
               //console.log("Lastname5: ", doc.lastname.value());
               //console.log("Lastname6: ", doc.lastname.value);

               var mydata = JSON.parse(doc.data());
               var lname = mydata['lastname'];
               console.log("Lastname7: ", lname);
              
               //document.getElementById("b-lastname").value = doc.data.lastname.value();
           } else
           console.log("Document not found!!");
       })```

The doc object contains the document data (retrieved by calling doc.data() ) as well as other properties (like the id ). So in order to access the value of certain attributes within the data you need to do doc.data().property where property is the key for the property you want to access.

So to access the lastname, you would need to do doc.data().lastname

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM