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, to cross-check, it returns 1 again, instead of the updated value(i.e. 4).

I want to use the value I'm getting from the database, in the global namespace. (use it simply in my JS code outside this function.)

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); // (to crosscheck) logs " Updated Value : 1 " but should be equal to 4 

I've been into JS for only a month, I'm quite a beginner. Some of the solutions I thought-&-tried were.

  1. Use order variable outside the function straight-away, like I did above, which didn't work.
  2. Assign the value of order to a new variable in the global namespace, which didn't work either.

I want to use this value outside in my global namespace.

Basically, I want to use the order variable outside, in my code. Also, I'll use this variable again, for naming other documents in my Firestore Database. In simple terms, It's necessary for me to have this value as a simple global var variable in my script.

Please help me with what I'm doing wrong or what this code is missing.

Raghav
  • 17
  • 4
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Keith Feb 29 '20 at 15:10
  • No, my friend, It doesn't. 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 29 '20 at 15:15
  • I'm on mobile, so not able update your code atm. But basically return your promise `return db.collection....`,. And the do `getfield.then(..` – Keith Feb 29 '20 at 15:30
  • Thw problem with your code is not about scope but about **timing**. The last line will execute before the variable is set. So, your code does bring the value successfully into global scope. Without knowing more about what you plan to do with this value it’s hard to provide a specific solution. I recommend to again read the answers in the linked question or provide more information. – Felix Kling Feb 29 '20 at 18:13
  • @FelixKling here, have a look at this - [link to image](https://ibb.co/m9YvBvr). – Raghav Mar 02 '20 at 14:39

0 Answers0