I'm currently working on an App in Firebase, utilizing FireStore as my primary Database.
Below is a snippet of code where I define a variable order
and set it to a value of 1.
Afterwards, I update the value to 4 and use console.log
to verify. Everything checks out fine.
However, after running the function and logging the variable again, it reverts back to 1 instead of retaining the updated value.
This is My code (Please pay attention to the //comments)
console.log("Initial Value : " + order); // logs 'Initial Value : 1'
//A function that retrieves another value from FireStore Database and assigns it to the variable.
function getField() {
db.collection("index")
.doc("artNum")
.get()
.then(function(doc) {
order = doc.data().artNum; //Reassigning the variable to '4' here.
console.log("Value assigned : " + order); // logs 'Value assigned : 4'
})
.catch(err => {
console.log(err);
});
}
getField();
console.log("Updated Value : " + order); // logs " Updated Value : 1 " when it should be 4
I would greatly appreciate any assistance in identifying what's causing this discrepancy in my code or if there's something crucial missing.