0

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.

Raghav
  • 17
  • 4

1 Answers1

0

You can just do window.order = yourValue (replace window with global if you are in node) to create a global order variable.

You also have to understand that your code is asynchronous which means that the updates will happen after your getField function is called. So looking for the new value will not work. However your getFields function returns a Promise that is always fulfilled (thanks to your catch clause).

So this should work

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() {
  return 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().then(() => console.log("Updated value", order)); 
adz5A
  • 2,012
  • 9
  • 10
  • Thanks for your answer friend. Basically I'm a newbie in web Development, and of course Javascript. And I'm not able to integrate this in my code. Can you please update my code with this method. I'll be really thankful. – Raghav Feb 27 '20 at 11:43
  • I read your code a bit fast and did not catch the async flow you are working with. My update should work. I suggest you look at the docs for Promises and chaining them and global variables (https://frontendmasters.com/courses/deep-javascript-v3/dynamic-global-variables/) – adz5A Feb 27 '20 at 12:30
  • It's showing this error : [https://i.ibb.co/5230w46/Fullscreen-capture-27-02-2020-190931.jpg](https://i.ibb.co/5230w46/Fullscreen-capture-27-02-2020-190931.jpg) or [https://ibb.co/st4fzR2](https://ibb.co/st4fzR2) . Uncaught TypeError: Cannot read property 'then' of undefined at app.js:80 – Raghav Feb 27 '20 at 13:44
  • I edited, you need to add the `return` clause in the `getField` function. sorry – adz5A Feb 27 '20 at 15:46
  • Thanks buddy, This seems to work. But I wanted to use the value of the `order` variable outside the function. – Raghav Feb 28 '20 at 13:11