Write user to firebase database

Asked

Viewed 100 times

0

Good afternoon!

I am facing a problem to record my user in my firebase database Altime. The scenario is as follows :

I have my user creation code using the createUserWithEmailAndPassword method, according to the firebase documentation, after this method is executed it returns an object that in this case is the user. The problem is when I try to access the uid of this user created to record it in the Altime database, because when I try to access this uid an error is returned saying that uid is undefined, but if I give a console.log(user) I can view this uid. What can I do to solve this problem ?

Follow the code:

   const firebase = require('firebase');


// Your web app's Firebase configuration
const firebase_config = {

};
// Initialize Firebase
firebase.initializeApp(firebase_config);

let user = {
    uid: '',
    displayName: 'teste',
    email: '[email protected]',
    password: '11111111',
    roles: ['admin','user'],
}

firebase
    .auth()
    .createUserWithEmailAndPassword(user.email,user.password)
    .then((newUser) =>{
        console.log(newUser)
        user.uid = newUser.uid

    firebase
        .database().ref('users/'+user.uid)
        .update(user)
        .then(()=>{
        console.log('usuario ' +user.displayName + ' criado')
    })
}).catch(console.log)

This is the error generated : Error: Reference.update failed: First argument contains Undefined in Property 'users.undefined.uid'

1 answer

-1


Change the code snippet:

user.uid = newUser.uid

For:

useruid = newUser.key

Browser other questions tagged

You are not signed in. Login or sign up in order to post.