0

noob question regarding Firestore. I am new to firebase and I am creating a simple app but could not get to show the profile name or any subcollection on my dashboard the only thing showing was the email address.

Login and signup has no problem as the data are already being stored on the firestore database. only my dashboard is the problem I cannot display anything that is inside the subcollection.

Console log has no problem as well it is showing me the user ID and the subcollection list. It doesn't show on my dashboard. Here is the sample code.

     const curUser = firebase.auth().currentUser;
     const { email } = curUser;

     const dbUser =
      firebase
        .firestore()
        .collection('users')
        .doc(curUser.uid)
        .get()   
        .then(doc => {
           if (!doc.exists) {
             console.log('No such document!');
           } else {
              console.log(doc.id, doc.data());
           }
        })      

  const { firstname, lastname, bio } = dbUser;
  this.setState({ firstname, lastname, bio, email });
 };


  render() {
     return (

     <View >
              <Text>
                 {this.state.firstname} {this.state.lastname}
              </Text>

              <Text>
                 {this.state.bio}
              </Text>

              <Text>
                 {this.state.email}
              </Text>
     </View>
    )
  }
};

firstname. lastname, and bio is showing blank. I don't know if I'm doing it right. Any help would be much appreciated. Thank you in advance.

1 Answers1

0

The get() method is asynchronous and returns a Promise.

When this Promise is fulfilled, its result is passed to the success handler in the then() function.

Therefore you need to do as follows:

 firebase
    .firestore()
    .collection('users')
    .doc(curUser.uid)
    .get()   
    .then(doc => {
       if (!doc.exists) {
         console.log('No such document!');
       } else {
          console.log(doc.id, doc.data());
          const { firstname, lastname, bio } = doc.data();
          this.setState({ firstname, lastname, bio, email });
       }
    })      
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • somehow I am getting an error "TypeError: undefined is not an object (evaluating 'dbUser.firstname')" showing only on the console. It doesn't show on the screen and I can still navigate through the app and edit the field but everytime it mounted there will always be an error message on the console. – code_error Mar 30 '20 at 09:14
  • Sorry but it is not possible to help you as we don't know what is `'dbUser.firstname`. You should most probably ask a new question (since it seems to be a new problem) and provide a [minimal, complete and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). – Renaud Tarnec Mar 30 '20 at 09:18
  • the example was posted above, sorry i'll just post a new question – code_error Mar 30 '20 at 09:21