[英]Angularfire2 Firestore Update same field in multiple documents
I have a profile table and a jobs table stored on Firebase Firestore.我有一个配置文件表和一个工作表存储在 Firebase Firestore 上。 I am creating an Angular 5 web site.
我正在创建一个 Angular 5 网站。 To save on network calls i added the users profile name to each job document.
为了节省网络调用,我将用户配置文件名称添加到每个作业文档中。 (Not sure if firestore has table linking).
(不确定 firestore 是否有表链接)。 So when the user updates him name, I also have to update their job documents (not many).
所以当用户更新他的名字时,我也必须更新他们的工作文件(不是很多)。 I can get a collection of jobs with the right user id.
我可以获得具有正确用户 ID 的作业集合。 But i cannot figure out how to iterate through the list and update the one field without using an Observable (one time call).
但是我无法弄清楚如何在不使用 Observable(一次调用)的情况下遍历列表并更新一个字段。 Here is what I tried that forced my browser into a continous loop
这是我尝试强制浏览器进入连续循环的尝试
jobsCollection : AngularFirestoreCollection<Job>;
jobDoc: AngularFirestoreDocument<Job>;
jobs: Observable<Job[]>;
job: Observable<Job>;
update(profile: Profile){
this.jobsCollection = this.db.collection('jobs', ref => {
return ref.where('user_id', '==',profile.user_id);
});
this.tracksCollection.valueChanges(item => {
console.log('update this record:', item);
item.forEach(job=> {
console.log('This is the job');
this.jobDoc = this.db.doc(`jobs/${job.id}`);
job.name= profile.name;
this.jobDoc.update(job);
});
});
}
Any help would be greatly appreciated, thanks PK任何帮助将不胜感激,谢谢 PK
You need a .subscribe()
to get the data from this.jobsCollection
and iterate through each and update the doc like 您需要一个
.subscribe()
来从this.jobsCollection
获取数据并遍历每个对象,并像这样更新文档
this.jobsCollection.snapshotChanges().map(changes=>{
return changes.map(a=>{
const data = a.payload.doc.data();
const id = a.payload.doc.id;
return {id, ...data}
})
}).subscribe(items=>{
items.forEach(job=>{
this.db.doc(`jobs/${job.id}`).update({name:profile.name});
})
});
I assume you have a filed called id
inside each doc under jobs
collection, otherwise you need to use .snapshotChanges()
instead of .valueChanges()
我假设你有一个名为申请
id
下的每个文档里面jobs
集,否则,你需要使用.snapshotChanges()
代替.valueChanges()
You can do it like that 你可以那样做
this.jobsCollection.get().forEach((item) => {
return item.docs.map(m => {
return this.db.doc(`jobs/${m.id}`).update({name:profile.name});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.