ronak patel
ronak patel

Reputation: 418

Uncaught Error: Reference.set failed: First argument contains undefined in property 'users.undefined.email'

I want to save a new node or object users to firebase database, but it gives me the error first argument contains undefined. I am unable to fix this error. Can anybody tell me where am I going wrong?

class Authen extends Component {

    login(event) {
        const email = this.refs.email.value;
        const password = this.refs.password.value;
        console.log(email,password);

        const auth = firebase.auth();
        const promise = auth.signInWithEmailAndPassword(email, password);

        promise.catch((e) => {
            var err = e.message;
            console.log("The error is ",err);
            this.setState({err: err});
        });
    }

    signup(event) {
        const email = this.refs.email.value;
        const password = this.refs.password.value;
        console.log(email,password);

        const auth = firebase.auth();
        const promise = auth.createUserWithEmailAndPassword(email,password); 

        promise
        .then(user => {
            var err = "Welcome "+user.email;
            firebase.database().ref('users/'+user.uid).set({
                email: user.email
            });
            console.log("The user is ",user);
            this.setState({err: err});
        });
        promise
        .catch(e => {
            var err = e.message;
            console.log(err);
            this.setState({err: err});
        });
    }

    constructor(props){
        super(props);

        this.state = {
            err: ''
        };
        this.login = this.login.bind(this);
        this.signup = this.signup.bind(this);
    }

    render() {
        return (
            <div>
                <input id="email" ref="email" type="email" placeholder="Enter your email" /><br />
                <input id="pass" ref="password" type="password" placeholder="Enter your password" /><br />
                <p>{this.state.err}</p>
                <button onClick={this.login}>Log In</button>
                <button onClick={this.signup}>Sign Up</button>
            </div>
        );
    }
}

export default Authen;

Upvotes: 2

Views: 758

Answers (2)

johannesMatevosyan
johannesMatevosyan

Reputation: 2208

I had this error and the reason is that received promise used in wrong way. You need to go deeper and use email and uid properties inside user object in response.

In other words, change

  1. user.email to user.user.email

  2. user.uid to user.user.uid.

    var err = "Welcome "+ user.user.email firebase.database().ref('users/' + user.user.uid).set({ email: user.user.email });

Upvotes: 1

Galbeeyutd Mok
Galbeeyutd Mok

Reputation: 51

I had a similar error, I think firebase has some other error and assumes you are passing through a .child which leads to the reference error.

Try setting email to be a string and then retyping user.email.

Upvotes: 0

Related Questions