0

I have the following function:

export function readUserData(userName, password) {
  firebase.database().ref("users/" + userName).once("value").then(function (snap) {
    //console.log(snap.val().salt);
    var verification = passwordVerify(password, snap.val().salt);
    //console.log(verification);
    //console.log(snap.val().password);
    console.log(snap.val());
    return snap.exists() && snap.val().password === verification 
      ? userAuth.located = snap.val()
      : userAuth.located = false;
  });
  return userAuth.located;
}

I am told that firebase.database().ref is an asynchronous function, and it seems to be so in that it returns userAuth.located in the final line of readUserData before writing console.log(snap.val());
How do I ensure that firebase.database().ref... executes before I return the final result? I am unsure how to implement await/promises into this code as the overall function is not asynchronous.

1
  • It's the calling code that will still need to deal with the async nature of this function. Commented Dec 9, 2019 at 21:28

2 Answers 2

2

You should return the promise, like:

export function readUserData(userName, password) {
    return firebase.database().ref("users/" + userName)
      .once("value")
      .then(function (snap) {
        var verification = passwordVerify(password, snap.val().salt);
        return snap.exists() && snap.val().password === verification 
          ? snap.val()
          : false;
    });
}

Then, whichever code calls this method, should also async-await:

var userAuthLocated = await readUserData(userName, password);

or, with promises:

readUserData(userName, password)
  .then(userAuthLocated => {
    // use userAuthLocated here
  });
Sign up to request clarification or add additional context in comments.

2 Comments

When I execute it the first way, it is passing as undefined in userAuthLocated as there is a return to the firebase.database() function but not one for readUserData. I am unsure how to use promises in javascript.
Hi @Seraphim. It looks like the return in the original readUserData function wasn't actually returning anything. I've updated it. Please try now.
0

To address your comment above @Seaphin. There is one more step needed with the following line

var userAuthLocated = await readUserData(userName, password);

What is happening is that your app is using userAuthLocated before it updates. userAuthLocated does get updated by the firebase call in readUserData, however, you are missing an async() function.

Using await should always be wrapped in an async () function call. It should look something similar to this

userAuth = async () => {
    var userAuthLocated = await readUserData(userName, password);
    //setstate or
    return userAuthLocated

Essentially using async "blocks" until the value is ready to use. Functions waiting on that value won't execute until the async function unblocks

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.