【问题标题】:Angularfire undefined variableAngularfire未定义的变量
【发布时间】:2018-04-21 11:14:35
【问题描述】:

我尝试使用 angularfire2 从 firebase 获取我当前的用户存储在实时数据库中

  public user: any;
  public userRef: any
  public currentUser: any;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private fireAuth: AngularFireAuth,
    private userProvider: UserProvider,
    private fireDateBase: AngularFireDatabase
  ) {
    this.currentUser = this.fireAuth.auth.currentUser.uid;
    this.userRef = this.fireDateBase.object('CITYNAV_USERS/' + this.currentUser);
    this.getCurrentUser();
  }

  ionViewDidLoad() {
  }

  public getCurrentUser(): void {
    this.userRef.snapshotChanges().subscribe(action => {
      this.user = action.payload.val();
      console.log(this.user);
    });
  }

但由于某种原因,this.user 告诉它是未定义的,我不知道为什么。任何想法。

HTML

<ion-content padding>
  <div class="wrapper-profile">
    <img src="assets/imgs/logo-f.png" alt="">
    <div class="wrapper-avatar">
      <div class="avatar">
        <img src="{{user.avatar}}" alt="avatar">
      </div>
      <button ion-button color="icons-color" clear>Cambiar imagen</button>
    </div>
  </div>
  <div class="wrapper-profile-form">
    <ion-item>
      <ion-label>
        <ion-icon name="ios-person-outline"></ion-icon> Nombre</ion-label>
      <ion-input clearInput type="text" [(ngModel)]="user.name"></ion-input>
    </ion-item>
    <ion-item>
      <ion-label>
        <ion-icon name="ios-mail-outline"></ion-icon> Email</ion-label>
      <ion-input clearInput type="email" [(ngModel)]="user.email"></ion-input>
    </ion-item>
    <ion-item>
      <ion-label>
        <ion-icon name="ios-lock-outline"></ion-icon> Password</ion-label>
      <ion-input clearInput type="password" [(ngModel)]="user.password"></ion-input>
    </ion-item>
    <div class="profile-btn">
      <button class="btn-edit" ion-button color="secondary" round>Editar informacion</button>
      <button class="btn-logout" ion-button color="secondary" (click)="logOut()" round>Log Oout</button>
    </div>
  </div>
</ion-content>

这是从实时数据库中获取用户对象后渲染用户对象的html。

控制台记录响应对象

    Object { 
      avatar: "assets/imgs/avatar.jpg", 
      created: "Wed Nov 08 2017 16:54:53 GMT+0100", 
      email: "mianfrigo@gmail.com", 
      password: "Atenas10", 
      rol: "user", 
      userName: "Miguel Frias" 
    }

【问题讨论】:

  • 没有人知道可能出了什么问题...?
  • 您的传入数据是什么样的?如果您按照答案中的建议将数据控制台记录在订阅中,数据是否存在?
  • 是的,我在控制台中记录了订阅中的数据并且数据就在那里。
  • 它看起来怎么样?请将 JSON 格式复制粘贴到您的问题中。
  • 那是我从响应中得到的控制台日志对象,但奇怪的是我从控制台中得到了未定义,我不知道为什么,因为我将响应分配给this.user,即一个公共变量。所以我真的迷路了。

标签: angular firebase ionic-framework angularfire2


【解决方案1】:

现在,当您进行了适当的更改以将控制台日志放入回调 (subscribe)...

您可以初始化您的user 或在模板中使用*ngIf,因为数据是异步的,视图会在数据到达之前呈现。或者您可以使用安全导航运算符,但安全导航运算符不适用于双向绑定,因此您需要将其拆分为单向绑定和ngModelChange

<input [ngModel]="user?.name" (ngModelChange)="user.name = $event"/>

或者如前所述,您可以使用 ngIf 并将您的模板包装在一个 div 中,例如:

<div *ngIf="user">
  <!-- code here-->
</div>

或者也许最简单的解决方案就是初始化对象。如果您的对象中没有嵌套对象,这将起作用。所以只需初始化你的用户:

user = {};

【讨论】:

  • 仅用*ngIf 包装模板就足够了,非常感谢@AJT_82 的帮助
【解决方案2】:

你需要把console.log放在subsrcibe里面,

public getCurrentUser(): void {
    this.userRef.snapshotChanges().subscribe(action => {
      this.user = action.payload.val();
      console.log(this.user);
    });

  }

【讨论】:

  • 是的,我知道,但是当我让用户对象在 HTML ionic 上呈现时,告诉我用户未定义。
  • 尝试使用安全导航运算符
  • 不,它没有用。我不知道问题出在哪里。
猜你喜欢
  • 2017-12-12
  • 2015-05-07
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
  • 2021-02-27
  • 2020-06-27
  • 2015-06-01
相关资源
最近更新 更多