2
votes

I am currently making an app in react-native and would like to link userid generated from firebase authentication to user information in my firebase database.

{
  users:useruid:{
                  //Storing other user information
               }
}

I am retrieving the uid from auth and creating a new child for the users like this:

firebase.auth().onAuthStateChanged((user) => {

  console.log(user)

  if(user){

    user.getToken().then((userid) => {
      //Set the initial state
      firebase.database().ref('/users/').child(userid).set({
        'uid': userid,
        'businessName': business,
        'street address': street,
        'city': city,
        'state': state,
        'zipcode': zipcode,
        'productGroup': 0,
        'pom': 0,
        'som': 0,
        'inventory': 0,
        'reporting_and_analytics': 0,
        'accounting': 0
      })

      dispatch({type: types.REGISTER_USER_SUCCESS})

    }).catch(function(error){
      console.log(error.message)
      dispatch({type: types.REGISTER_BUSINESS_FAILURE, error});
    });
  }
  else{
    console.log("ERROR: Registration error")
  }
});

 }

I am getting the error: Invalid path with regards to the uid generated. Any help. I have checked documentation on firebase and similar questions related to this but still getting the same error.

Error

1
use uid instead of token, like firebase.database().ref('/users/').child(user.uid) - Ivan Chernykh
@cherniv I am getting the same error with user.uid. Also on the documentation user.getToken() is recommended rather than user.uid. - dorah
@Cherniv user.uid works. After several retries, it worked. user.getToken() generates a token with some characters that cannot be used on a firebase path. firebase Invalid Characters - dorah
I'm struggling with the same problem. I realise your post is a couple of years old - so maybe things have changed, but I can't get this approach to work. are you still using in this way? - Mel

1 Answers

0
votes

The path:

firebase.database().ref('/users/').child(userid)

is essentially the same as saying //users//userid because of the way child() works, which is obviously an incorrect path.

Instead lose the / before and after users. Try:

firebase.database().ref('users').child(userid)

The child() methods will form the correct path.