升级Firebase期间的UpdateProfile()

Tri*_*apy 6 firebase angularfire firebase-authentication ionic2 angular

我正在开发一个应用程序,我想实现这一目标。当用户登录时,我想获取其用户名并存储在firebase中。所以我创建了这个:

Signup(email: string, password: string, displayName: string) {
    this.angFire.auth.createUser({
      email: email,
      password: password
    }).catch(
      (err) => {
        console.log(err);
        this.error = err;
      })
...
Run Code Online (Sandbox Code Playgroud)

并且这可行,现在我还需要将displayName保存到当前用户中,因此我尝试在同一函数(Singup())中执行此操作:

let user = firebase.auth().currentUser;
    user.updateProfile({
      displayName: this.displayName,
      photoURL: "https://example.com/jane-q-user/profile.jpg"
    }).then(function() {
      console.log("Nome utente inserito");
    }, function(error) {
      console.log("Errore durante inserimento utente");
    });
Run Code Online (Sandbox Code Playgroud)

但这给了我这个:

error_handler.js:54 EXCEPTION: Error in ./SignupPage class SignupPage - inline template:70:4 caused by: Cannot read property 'updateProfile' of null
Run Code Online (Sandbox Code Playgroud)

怎么做?我在ionic 2 angular2,firebase / angulrfire上运行

文档参考:

更新用户资料

您可以使用updateProfile方法更新用户的基本个人资料信息(用户的显示名称和个人资料照片URL)。例如:

var user = firebase.auth()。currentUser;

user.updateProfile({显示名: “简问用户”,photoURL: “ https://example.com/jane-q-user/profile.jpg ”}),然后(函数(){
//更新成功} ,function(error){//发生错误。});

这里

bri*_*mcq 1

我的回答太晚了,但万一有人感兴趣,你可以这样做

  constructor(
    private afAuth: AngularFireAuth,
    private router: Router
  ) { 
   //
  }


async signupUser(email: string, password: string): Promise<any> {
    const data = await this.afAuth.createUserWithEmailAndPassword(
      email,
      password
    );
    return this.updateUserData(data.user).then(() => {
      this.router.navigate(["/"]);
    });
  }

 private updateUserData({ uid, email, photoURL, displayName }: User) {
    const userRef: AngularFirestoreDocument<User> = this.afs.doc(
      `users/${uid}`
    );
    return userRef.set({ uid, email, photoURL, displayName }, { merge: true });
 }
Run Code Online (Sandbox Code Playgroud)

您可以在这里了解更多信息