vendredi 14 mai 2021

How to assign data fetched from Firestore.collection() .get() .then() method to a global variable

I want to fetch data (a particular field in a doc) from Firebase FireStore → Assign that value to global variable → console.log that value.

My Research till now:

  1. I tried assigning the value to global variable → NOT WORKING
  2. I tried returning the fetched value

var a = firestore.collection("collection1").doc("doc1").get().then(){ return doc.data().FIELD}

  1. removing the return keyword from the above

Currently I am doing nested code like console.log() inside the then().

What my code looks like now

var a = "BEFORE";
console.log(a); // BEFORE
firestore.collection("collection1").doc("doc1").then(
  function(doc) {
    const myData = doc.data();
    a = myData.Reciept_Series;
    console.log(a); // DESIRED_VALUE
  }
)
console.log(a); //BEFORE

How can I make a = DESIRED_VALUE ie. How can I use the value that I got from FireStore .

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire