0

I'm new firebase database, I try to update UID to Realtime Database when signup, but got an error like this

Error: Reference.update failed: First argument contains undefined in property 'users.UID.email'

auth.service.ts

import ...
...
export class AuthService { 
...

 async emailSignUp(email: string, password: string) {
    try {
      const user = await this.afAuth.auth.createUserWithEmailAndPassword(email, password);
      console.log('user ', user);
      this.authState = user;
      console.log('authState ', this.authState);
      this.updateUserData();
      this.router.navigate(['/']);
    } catch (error) {
      return console.log(error);
    }
  }

 get currentUserId(): string {
    console.log('currentId ', this.authenticated, ' ', this.authState.user.uid);
    return this.authenticated ? this.authState.user.uid : '';
  }

 private updateUserData(): void {
    const path = `users/${this.currentUserId}`; // Endpoint on firebase
    const userRef: AngularFireObject<any> = this.db.object(path);
    const data = {
      email: this.authState.email,
      name: this.authState.displayName
    };
    userRef.update(data)
      .catch(error => console.log(error));
  }

}

Console enter image description here

Can somebody help me, please? please let me know if more snippets are needed. Thanks in advance.

1 Answer 1

1

The error is telling you that this.authState.email is undefined. It's not valid to pass an undefined value to Realtime Database. You should check that before using it this way.

I'm seeing that you assigned this.authState from user in your constructor. Your logged user in the console, and it doesn't have a property called email, so this error does not surprise me at all. But it does have a property also called user. So, perhaps you meant this.authState.user.email instead.

Sign up to request clarification or add additional context in comments.

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.