ums2026
ums2026

Reputation: 147

Get one user's data in firestore and display in Expo

I am using Expo and Firebase to create an app. I am currently using this code to get the user's profile information:

async function getUserInfo(){
    try {
      let doc = await firestore
        .collection('users')
        .doc(auth.currentUser.uid)
        .get();

      if (!doc.exists){
        alert('No user data found!')
      } else {
        let dataObj = doc.data();
        alert(dataObj.FullName);
      }
    } catch (err){
    alert('There is an error.', err.message)
    }
  }

This way, it works. The alert is displayed. However, I am trying to display the value like this

   <Text>Name: {here_some_variable_for_name}</Text>

Here's what I have tried:

  const [fullName, setFullName] = useState('')

  async function getUserInfo(){
    try {
      let doc = await firestore
        .collection('users')
        .doc(auth.currentUser.uid)
        .get();

      if (!doc.exists){
        alert('No user data found!')
      } else {
        let dataObj = doc.data();
        setFullName(dataObj.FullName);
      }
    } catch (err){
    alert('There is an error.', err.message)
    }
  }

  getUserInfo();


After adding the line "const [fullName, setFullName] = useState('')", the code suddenly doesn't work. When testing it in the web browser, the page is just blank.

Neither of these have worked. The data is definitely being received correctly, but the displaying part is not working.

Upvotes: 0

Views: 1115

Answers (1)

ums2026
ums2026

Reputation: 147

Found the solution: I had forgotten to import useState, I did that and it worked.

Upvotes: 1

Related Questions