3

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
  • What library are you using? Commented Nov 15, 2017 at 21:19
  • It looks like the problem is around user.uid, can you console.log that and also console.log the firebase.database().ref('users/' + user.uid) and see what you get. Commented Nov 15, 2017 at 21:30
  • Will try and get back to you Commented Nov 16, 2017 at 12:36
  • @mjwatts I tried the console.log and the first one was to log the user.uid and I got it. The second one was to the url: urltofirebase/users/the same uid as the first console.log. I placed the console.logs in the first then and just before the firebase. Commented Nov 16, 2017 at 15:39

4 Answers 4

5

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
    })
})
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1
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!

1 Comment

Please put your answer always in context instead of just pasting code. See here for more details.
0

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';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.