core.js:6150 ERROR TypeError: this.object is not iterable in Angular

core.js:6150 ERROR TypeError: this.object is not iterable in Angular

我想在 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;
  }

但是当 First Name 显示在 HTML

中时编译器出错
core.js:6150 ERROR TypeError: this.users is not iterable

不要使用与订阅对象和数据相同的变量 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;
      }
    })
  }