错误-使用Ionic V3更新Firebase数据库中的用户名

时间:2018-11-17 09:45:44

标签: firebase ionic-framework ionic3 firebase-authentication

我想在firebase中添加用户名字段,因为所有人都说我们不能在firebase中添加直接用户名字段。我试图通过更新方法添加。如果我以这种方式在代码中添加值,但我不知道如何通过输入来添加值,这就是我的代码。

file.ts

this.fAuth.auth.onAuthStateChanged(function (user) {
  if (user) {
    // Updates the user attributes:
    user.updateProfile({ // <-- Update Method here
      displayName: "Goku",
      photoURL: "https://example.com/jane-q-user/profile.jpg"
    }).then(function () {
      // Profile updated successfully!
      //  "NEW USER NAME"
      var displayName = user.displayName;
      // "https://example.com/jane-q-user/profile.jpg"
      var photoURL = user.photoURL;
    }, function (error) {
      // An error happened.
    });
  }
});

file.html

<ion-content>
<form [formGroup]="myForm">
  <ion-list>
    <ion-item>
      <ion-label floating>Username</ion-label>
      <ion-input formControlName="displayName" type="text" [(ngModel)]="user.displayName"></ion-input>
    </ion-item>
</form>
</ion-content>

请帮助我...

1 个答案:

答案 0 :(得分:0)

要从html中获取值,请尝试以下操作:

<form [formGroup]="myForm" (ngSubmit)="onSubmit()">
 <ion-list>
  <ion-item>
    <ion-label floating>Username</ion-label>
    <ion-input formControlName="displayName" type="text" [(ngModel)]="displayName"></ion-input>
  </ion-item>
  <ion-item>
    <button ion-button type="submit" [disabled]="myForm.invalid">Submit</button>
  </ion-item>
 </ion-list>
</form>

然后在.ts中,执行以下操作:

ngOnInit()
{
 this.myForm = this.fb.group({
  displayName : ['', Validators.required],
 });
}

onSubmit()
{
   if(this.myForm.valid)
   {
     this.displayName = this.myForm.get('displayName').value;
     console.log(this.displayName);
   }
} 

this.displayName将包含ion-input的值,不要忘记在类声明下声明属性displayName