TypeError:无法读取未定义Firestore的属性'uid'

时间:2018-06-19 23:04:11

标签: angular typescript firebase firebase-authentication google-cloud-firestore

我正在尝试在Firebase中创建用户,但出现以下错误:TypeError: Can not read property 'uid' of undefined

换句话说,如果您创建用户但不写文档。

interface User {
uid: string;
email: string;
photoURL?: string;
displayName?: string;
favoriteColor?: string;
fechaRegistro?: string;
}

emailSignUp(email: string, password: string, displayName: string ) {
this._displayName = displayName;

return this.afAuth.auth
  .createUserWithEmailAndPassword(email, password)
  .then(credential => {
    this.pushUserDataEmail(credential.user);
    console.log('Usuario creado')
    this.router.navigate(['/userProfile'])
  })
  .catch(error => {
    this.handleError(error)
  });
}

private pushUserDataEmail( user: User ) {
const userRef: AngularFirestoreDocument<User> = this.afs.doc(`users/${user.uid}`);
const data: User = {
  uid: user.uid,
  email: user.email,
  displayName: this._displayName
};
userRef.set(data, {merge: true});

}

但是,当我在另一个应用程序中尝试相同的代码时,它可以正常工作,即先创建用户,然后编写文档。

0 个答案:

没有答案