【问题标题】:core.js:6150 ERROR TypeError: this.object is not iterable in Angularcore.js:6150 错误类型错误:this.object 在 Angular 中不可迭代
【发布时间】:2021-03-14 00:23:43
【问题描述】:

我想在 HTML 文件中显示用户的名字

  getFirstName(id: any){
    this.users = this.afs.collection('users', ref => ref.where("uid", "==", id)).valueChanges();

    this.users.subscribe(users => {
      this.users = users;
    })
    let fname;
    for(let user of this.users){
        fname = user.firstName;
    }
    return fname;
  }

但是当名字在 HTML 中显示时编译器会出错

core.js:6150 ERROR TypeError: this.users is not iterable

【问题讨论】:

    标签: javascript angular firebase google-cloud-firestore angularfire2


    【解决方案1】:

    不要使用相同的变量 this.users 作为订阅对象和数据。您还需要在订阅中使用 for-of。

    fname  = null;
    
      getFirstName(id: any){
        this.users = this.afs.collection('users', ref => ref.where("uid", "==", id)).valueChanges();
    
        this.users.subscribe(users => {
          for(let user of users){
            this.fname = user.firstName;
          }
        })
      }
    

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-10
      • 2021-10-28
      • 2012-10-30
      • 1970-01-01
      • 2022-11-29
      相关资源
      最近更新 更多