[英]How to add a collection to another in cloud firestore?
In cloud firestore, I want to add the value form to collection A and then all the same value to another collection B, but collection B is inside collection A. Currently I want to get the id of the document while adding the data to collection A and then use this.firestore.collection(A).doc(id).collection(B).add(data), but currently I cannot get the ID of the document This one is my code to get ID when adding data to collection A.在cloud firestore中,我想将值形式添加到集合A,然后将所有相同的值添加到另一个集合B,但集合B在集合A中。目前我想在将数据添加到集合A时获取文档的ID然后使用this.firestore.collection(A).doc(id).collection(B).add(data),但目前我无法获取文档的ID 这是我在将数据添加到集合时获取ID的代码一个。
onSubmit(form: NgForm) {
let data = form.value;
lastID = '';
this.firestore
.collection('Users')
.add(data)
.then(function (docRef) {
this.lastID = docRef.id;
});
this.firestore.collection('Users').doc(lastID).collection('profile').add(data);
this.resetForm(form);
}
You can do the second add inside the callback of the first.您可以在第一个回调中进行第二个添加。 Also note that you should use an arrow function in your callback to preserve the value of this
from the outer scope:另请注意,您应该在回调中使用箭头 function 以保留外部this
的值:
this.firestore
.collection('Users')
.add(data)
.then(docRef => {
this.lastID = docRef.id;
return this.firestore.collection('Users').doc(this.lastID).collection('profile').add(data);
})
.then(() => {
this.resetForm(form);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.