I have this function called getUserData which takes in the user's mobile and returns their userRecord object by checking it in Firebase Authentication.
I want the code to be such that when I call the getUserData('some mobile here'), it should return me the user's userRecord object from Firebase.
In the code below, it is returning undefined on calling the getUserData with correct arguments even if the user exists in the auth.
const getUserRecordFromAuth = mobile => {
const promise = auth.getUserByPhoneNumber(mobile);
/*
* userRecord: {
* uid: '52348348934534h5',
* name: 'ABC'
* mobile: '9090909090'
* }
*/
return promise.then(userRecord => {
return userRecord;
}).catch(error => {
console.log(error);
});
}
const getUserData = mobile => {
return userRecord = getUserRecordFromAuth(mobile)
.then(data => {
return data;
});
}
I think that the issue is with how promises work. Can someone please explain how I can do what I explained above? Any help would be appreciated.
This is Firebase API BTW.
Pankaj Patel
Blog, Tech, Photography etc.
In last part you can simply write:
const getUserData = mobile => getUserRecordFromAuth(mobile);And wherever you call
getUserData; use thethenchain because that way you will have assurance that it will either work or fail and in both cases, you can catch it.whole code can be simply re-written in few lines as:
const getUserRecordFromAuth = mobile => auth .getUserByPhoneNumber(mobile).catch(console.log); const getUserData = mobile => getUserRecordFromAuth(mobile);or without any abstraction:
const getUserData = mobile => auth .getUserByPhoneNumber(mobile) .catch(console.log);