mardi 20 août 2019

Why is firebase returning a pomise?

I have checked and read the other posts about making an asynchronous calls which tells me to handle the promise, this post is different because I am expecting my call to have already handled that

I am trying to get the current score of a specified user. However, instead of returning the score I am getting (in the console):

enter image description here

the current score in my firebase database is 3. Here is the function I have:

function getCurrentScore() {

    var userId = firebase.auth().currentUser.uid;
    return firebase.database().ref('/users/' + userId).child('score').once('value').then(function(snapshot) {
        console.log(snapshot.val());
    });

}

what I want to do is assign the value to a variable that I can use. For example

var score = getCurrentScore();

How can I get this value and assign it to a variable in firebase?

Edit for clarification:

So, I know that this is making an asynchronous call, however I thought that .then only works once that call has completed. Is this not the case? Does firebase handle this differently?

Here is what I thought from https://howtofirebase.com/promises-for-firebase-bbb9d0d595ed:

Returning a value out of a .then() callback and returning Promise.resolve() has the same result.

I am a noob junior so please do not crucify me. Extra points for detailed explanations that help me learn and increase my understanding..




Aucun commentaire:

Enregistrer un commentaire