Skip to content

Retrieve specific data from firebase Javascript

New to javascript here. I’m trying to pull a specific set of data from my firebase database and print it to my website but I am struggling to do so. The problem is it prints out every field in the database.

As you can see in this image, I would like the fields for just that particular ID printed out onto my website and not the fields for all the different ID’s [1]: https://i.stack.imgur.com/FIOcO.png

Here is what I have so far

var db = firebase.firestore();

const list_div = document.querySelector("#list_div");

db.collection("catalogue").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {


   list_div.innerHTML += "<h3>" + doc.data().Name + "</h3><p> Price: " + doc.data().Price +  "</p></div> Quantity" + doc.data().Quantity + doc.data().Description



});
});

Answer

The below will get data for id which you want:

    function getDataById(anyId) {
      db.collection("catalogue").doc(anyId).get()
      .then(function(doc) {
         if (doc.exits) {
            var desc = doc.data().Description,
            var name = doc.data().Name,
            var price = doc.data().Price
            var qty = doc.data().Quantity
         }
      }
    }