角度更新/在Firestore查询中添加项目

时间:2018-04-06 15:10:10

标签: angular firebase-realtime-database google-cloud-firestore angularfire2

我想在我的Firestore查询中更新/添加项目。

我尝试这样的东西,但(当然)结果没有更新。 我怎么能让这个。聊天者更新???

chatsuserCol: AngularFirestoreCollection<User>;
chatusers: Observable<User[]>;
public ChatUsers = (): Observable<User[]> => {
    this.chatsuserCol = this.afs.collection('users');
    this.chatusers = this.chatsuserCol.valueChanges();
    this.chatusers.subscribe(
        items => { 
            items.forEach(function (item, index) {
                item.displayName = 'Name : ' + item.displayName; // Update
                item.avatar = item.photoURL; // Add new field
            });
            alert(JSON.stringify(items));   // items has been updated
        }
    );
    return this.chatusers;  // I WANT TO UPDATE THIS
}

1 个答案:

答案 0 :(得分:0)

使用.map代替.subscribe。这将动态改变数据

尝试

this.chatsuserCol = this.afs.collection('users');
this.chatusers = this.chatsuserCol.valueChanges().map(users=>{
  users.forEach(user=>{
    user.displayName = 'Name : ' + user.displayName;
    user.avatar = user.photoURL;
  })
  return users;
});
return this.chatusers;