React native firebase creating user then adding additional data to realtime database

Viewed 7384

I am creating an app that allows users to register themselves.

Using react-native and firebase works, the are registered and are logged in. Even the additional data is stored in the realtime database (I checked it). But I am getting a message saying "creating user failed" (it is one of the messages I have setup).

This is the code I use to create users and add additional data to the realtime database:

firebase.auth().createUserWithEmailAndPassword(email, password)
    .then((user) => {
        firebase.database().ref('users/' + user.uid).set({
            firstName: firstName,
            lastName: lastName,
            email: email,
            code: code
        })
    })
    .then(user => loginUserSuccess(dispatch, user))
    .catch((error) => {
        createUserFail(dispatch)
        console.log(error);
    });

I also checked the debugger in android studio (don't know about ios yet) and saw the following comment:

TypeError: undefined is not an object (evaluating 'user.uid')

Still everything is saved, but the error message was shown. What am I doing wrong?

4 Answers

I know this is a year old but I ran into the same issue and here's a more direct solution for anyone running into the same issue. When you create a user the user object is a part of the Firebase response not the response itself. So just prepend "res" to your original code like this:

firebase.auth().createUserWithEmailAndPassword(email, password)
.then((res) => {
    firebase.database().ref('users/' + res.user.uid).set({
        firstName: firstName,
        lastName: lastName,
        email: email,
        code: code
    })
})

Ok, finally I got it working. What I did was, after the first then, checking for the currentuser (because the user will be loggedin automatically), then getting the uid and using that:

if (firebase.auth().currentUser) {
    userId = firebase.auth().currentUser.uid;
    if (userId) {
        firebase.database().ref('users/' + userId).set({
            firstName: firstName,
            lastName: lastName,
            email: email,
            code: code
        })
    }
}

This works.

firebase.auth().createUserWithEmailAndPassword(email, password).then((user)=>{
    if (firebase.auth().currentUser) {
      userId = firebase.auth().currentUser.uid;
      if (userId) {
          firebase.database().ref('users/' + userId).set({
            firstname:firstname,
            lastname:lastname,
            email:email,
            password:password,
            town:town,
            addInterest:addInterest,
            photoUrl:false,
            emailVerified:false,
            uid:userId,
            status:true,
            online:true
          })
      }
    }
  }).catch(function(error) {
    // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      console.log('Register!');
      console.log(error);
  })

Full Code so easy to use. 100% work!

You didn't say if you were using this in the context of an action creator but if you did, here is another possible solution. Let's make it more interesting, its an app for adding new employees:

export const employeeCreate = ({ name, phone, shift }) => {
  const { currentUser } = firebase.auth();

  return () => {
    firebase
      .database()
      .ref(`/users/${currentUser.uid}/employees`)
      // add a .then() to ensure after employee is created
      // we want to navigate them back to employee list screen
      .push({ name, phone, shift })
      .then(() => Actions.pop());
  };
};

The Actions would be imported from react-native-router-flux like so:

import firebase from 'firebase';
import { Actions } from 'react-native-router-flux';
Related