2

Im trying to assign variables to their respected value from the firestore database using the get doc function, I've noticed it does not assign or update the values what so ever.

I've tried to work with async and awaits but cannot seem to make it work.

getFromDatabase(nameOfCollection,nameOfDocument){

    const db = firebase.firestore();
    var docRef = db.collection(nameOfCollection).doc(nameOfDocument);
    docRef.get().then(function(doc) {
        if (doc.exists) {

         outvariable = doc.data().anyfield; // THIS IS WHAT I WANT
         console.log(" Document data:", doc.data());
        } else {
            console.log("No such document!");
        }
    }).catch(function(error) {
        console.log("Error getting document:", error);
    });

  }

im expecting outvariable = doc.data().anyfield

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Saad Ali
  • 23
  • 1
  • 3

2 Answers2

2

Most likely you're confused by the fact that data is loaded from Firestore asynchronously. It's not so much that the data isn't assigned to the values, because it really is. It just happens at a different time than you expect.

It's easiest to see this by adding some simple logging statements around the code that loads data:

const db = firebase.firestore();
var docRef = db.collection(nameOfCollection).doc(nameOfDocument);
console.log("Before starting to load data");
docRef.get().then(function(doc) {
  console.log("Got data";
});
console.log("After starting to load data");

When you run this code, the output is:

Before starting to load data

After starting to load data

Got data

This is probably not what you expected, but it's actually completely correct. The data is loaded from Firestore asynchronously (since it may take some time), and instead of waiting, the main code continues. Then when the data is available, your callback function is called with that data.

This means that any code that requires the data from the database must be inside the callback, or be called from there. So the console.log(" Document data:", doc.data()) in your original code should work fine. But a similar console.log outside of the callback won't work, because it runs before the data is available.

This is an extremely common source of confusion for developers new to this type of API. But since most modern web/cloud APIs, and many other APIs, are asynchronous, it's best to learn how to work with them quickly. For that, I recommend reading:

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1

The data can be extracted with .data() or .get() to get a specific field.

For example: doc.get(anyfield);

More info can be found on the official documentation.

sllopis
  • 2,292
  • 1
  • 8
  • 13
  • i tried it and the field variable does not assign to the variable, doc cannot be accessed outside the function :/ – Saad Ali Sep 11 '19 at 11:27
  • Have you tried checking first if the document exists? You can use the **[exists property](https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentSnapshot#exists)** to explicitly verify a document's existence. If you used the get or data properties, it must return 'undefined' if the document doesn't exist. Yes, doc only lives within the function, you would need to store the data into a variable or list, so you can have access outside the scope of the function. – sllopis Sep 11 '19 at 12:01
  • how would i store it into a list , i treid assigning it to a variable it didnt work , i also tried return the doc which also didnt work , could you modify the code to show me how please? – Saad Ali Sep 11 '19 at 13:06