I'm trying to display a username from a Firebase database in a React return function.
I have a function called getUserName
which retrieves the username from the database and returns it.
const getUserName = async () => { try { const docRef = doc(db, "users/" + auth.currentUser?.uid); const userDocument = await getDoc(docRef); const userData = userDocument.data() as UserData; const userName = userData.name.split(' ')[0]; return userName; } catch (error) { console.error(error) return "Failed to Retrieve UserName"; }; }
Then I try to display it in the return of the react component.
return ( <div> Hi, {getUserName()}! </div> )
But I get this error:
Type 'Promise
Not sure what I'm doing wrong here. I'm using TypeScript, so do I need to specify the type somewhere?
The error message is very clear. To resolve this issue, you can modify the
getUserName
function to use theuseState
hook to store theusername
and update it asynchronously.