Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Firestore的循环中使用update with batch方法_Javascript_Firebase_Google Cloud Firestore_Angularfire2 Offline - Fatal编程技术网

Javascript 在Firestore的循环中使用update with batch方法

Javascript 在Firestore的循环中使用update with batch方法,javascript,firebase,google-cloud-firestore,angularfire2-offline,Javascript,Firebase,Google Cloud Firestore,Angularfire2 Offline,在Firestore中,我尝试使用批处理方法更新循环中的文档 该代码是如何工作的: var batch = this.afs.firestore.batch(); var eventRef = this.eventCollection.doc(eventkey).ref; batch.update(eventRef, updateField ); var artistRef = this.memberCollection.doc('7yLf6RgLIIEUkAJ3jbXy').collectio

在Firestore中,我尝试使用批处理方法更新循环中的文档

该代码是如何工作的:

var batch = this.afs.firestore.batch();
var eventRef = this.eventCollection.doc(eventkey).ref;
batch.update(eventRef, updateField );
var artistRef = this.memberCollection.doc('7yLf6RgLIIEUkAJ3jbXy').collection('events').doc(eventkey).ref; 
batch.update(artistRef, updateField); 
var artistRef = this.memberCollection.doc('eJtcLZrUZhhObWcmptQs').collection('events').doc(eventkey).ref; 
batch.update(artistRef, updateField); 
batch.commit().then(function() {console.log('success')};
但这个不起作用:

var batch = this.afs.firestore.batch();
var eventRef = this.eventCollection.doc(eventkey).ref;
batch.update(eventRef, updateField );
if(artists) {
  Object.values(artists).forEach(function(artist) {
    var artistkey = artist.$key;
    var artistRef = this.memberCollection.doc(artistkey).collection('events').doc(eventkey).ref;
    batch.update(artistRef, updateField); 
  });
}
batch.commit().then(function() {console.log('success')};

它告诉我“ERROR TypeError:无法读取未定义的属性'memberCollection'”

因为您在回调函数内部,所以此的含义与它外部不同

最简单的解决方案是将
memberCollection
分配给回调之外的单独变量:

var batch = this.afs.firestore.batch();
var eventRef = this.eventCollection.doc(eventkey).ref;
var memberCollection = this.memberCollection;
batch.update(eventRef, updateField );
if(artists) {
  Object.values(artists).forEach(function(artist) {
    var artistkey = artist.$key;
    var artistRef = memberCollection.doc(artistkey).collection('events').doc(eventkey).ref;
    batch.update(artistRef, updateField); 
  });
}
batch.commit().then(function() {console.log('success')};
但您也可以对函数使用箭头语法,这可以防止重新分配此:

var batch = this.afs.firestore.batch();
var eventRef = this.eventCollection.doc(eventkey).ref;
batch.update(eventRef, updateField );
if(artists) {
  Object.values(artists).forEach((artist) => {
    var artistkey = artist.$key;
    var artistRef = this.memberCollection.doc(artistkey).collection('events').doc(eventkey).ref;
    batch.update(artistRef, updateField); 
  });
}
batch.commit().then(function() {console.log('success')};
这是一个非常常见的JavaScript问题,适用于任何有回调的地方(不仅仅是Firestore)。我建议您查看前面关于此主题的一些问题:


感谢弗兰克的回答。。。非常精确和完整!