Javascript core.js:6150 ERROR-TypeError:this.object在角度上不可编辑
我想在HTML文件中显示用户的名字Javascript core.js:6150 ERROR-TypeError:this.object在角度上不可编辑,javascript,angular,firebase,google-cloud-firestore,angularfire2,Javascript,Angular,Firebase,Google Cloud Firestore,Angularfire2,我想在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;
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
不要使用与订阅对象和数据相同的变量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;
}
})
}