score:0

Accepted answer

solved following: https://javebratt.com/firebase-user-undefined/

made asynchronous call because the uid was null for a few moment before fetching data, and error was triggered. just had to wait for data to load before error went off. also am grabbing the uid a new way via fire.auth().currentuser.uid.

code i changed in settings.js:

  componentdidmount(){
    // asynchronous call fixes uid being null on reload
    fire.auth().onauthstatechanged( user => {
        if (user) { 
            fire.database().ref('/users/' + fire.auth().currentuser.uid).once('value').then(function(snapshot) {
                this.setstate({
                  userfirstname: (snapshot.val().userfirstname),
                  userlastname: (snapshot.val().userlastname),
                  useremail: (snapshot.val().useremail),
                  userphone: (snapshot.val().userphone),
                  useraddress: (snapshot.val().useraddress),
                  userzip: (snapshot.val().userzip),
                  userprofilepicurl: (snapshot.val().userprofilepicurl)
                })
              }.bind(this));          
         }
      });

score:2

you should call your firebase connection in componentdidmount, but not in the constructor. you can read the details when to use react lifecycles. even though it is out of date (not react 16 article), it is a a great resource. https://engineering.musefind.com/react-lifecycle-methods-how-and-when-to-use-them-2111a1b692b1

class app extends react.component {
  constructor() {}
  componentdidmount() {
    // firebase call
    // do setstate
  }

  render() {
    return <div></div>
  }
}

Related Query

More Query from same tag