I'm creating an App in Firebase, using FireStore as my Database.
In the code below, I create a variable order
and assign it a value of 1.
Then I update the value to the number 4 and console.log
it to check. Which turns out fine.
But when I log the variable after the function, it returns 1 again, instead of the updated value.
This is My code (do See the //comments)
console.log("Value initiated : " + order); // logs 'Value initiated : 1'
//A function that gets another value from the FireStore Database and assigns it to the variable.
function getField() {
db.collection("index")
.doc("artNum")
.get()
.then(function(doc) {
order = doc.data().artNum; //I reassign 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 " but should be equal to 4
Please help me with what I'm doing wrong or what this code is missing.