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:
- I tried assigning the value to global variable → NOT WORKING
- I tried returning the fetched value
var a = firestore.collection("collection1").doc("doc1").get().then(){ return doc.data().FIELD}
- 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.