1

I'm trying to return the userRecord from inside the getUserRecordFromAuth function in firebase and to use it to get the user's UID from Authentication in Firebase.

The problem is that the userRecord.uid is always returning undefined, even though I'm returning the value of the promise in the getUserRecordFromAuth(). What gives?

const getUserData = mobile => {
    const userRecord = getUserRecordFromAuth(mobile);

    console.log('uid', userRecord.uid);
}

const getUserRecordFromAuth = mobile => {

    const promise = auth.getUserByPhoneNumber(mobile);

    return promise.then(userRecord => {
        return userRecord;
    }).catch(error => {
        console.log(error);
    });
}
Chandrika
  • 194
  • 12
Utkarsh Bhatt
  • 1,536
  • 2
  • 14
  • 23
  • `getUserRecordFromAuth(...).then(....)` – georg Mar 01 '18 at 09:52
  • You're not "returning the value of the promise" (which is impossible, as it will only become available in the future), but rather you are returning the promise itself. – Bergi Mar 01 '18 at 10:51
  • To achieve the same result in a more imperative looking fashion, similar to your code, you may also use [`asyc - await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) abstraction with promise returning async functions. – Redu Mar 01 '18 at 11:30
  • @Bergi Any propositons about the value returning promises that i can check. What would be difference from async/await..? – Redu Mar 01 '18 at 11:33

1 Answers1

2

As you are returning a Promise to get the result you need to use .then()

const getUserData = mobile => {
    getUserRecordFromAuth(mobile)
     .then((userRecord) => console.log('uid', userRecord.uid));
}
h1b9b
  • 1,010
  • 5
  • 16
  • 1
    What if I want to return the userRecord from the getUserData. Getting the value inside the `then()` method will only keep my userRecord in a limited scope. How do I return the whole userRecord? – Utkarsh Bhatt Mar 01 '18 at 16:59