angular - 如何批量添加到 Angular Firestore 中的集合?
问题描述
如何批量添加到 Angular Firestore 中的集合?
以下函数尝试在集合中创建新用户:
batchCreate() {
const batch = this.firestore.collection('users').ref.firestore.batch();
this.users.forEach((user) => {
const ref: any = this.firestore
.collection('users')
// even if reference a doc like this (it doesnot works):
// const ref: any = this.firestore
// .collection('users').doc(user.id)
//this collection and documents doesnot exists on the db.
batch.set(ref, user);
});
batch.commit()
}
但是上面的逻辑是行不通的。
不知道为什么!
这个想法是创建一个集合(它正式不存在但在旅途中创建),并在单个事务中推送整个用户列表,而不是单个发布请求。那可能吗?
这个想法是批量添加或更新集合
解决方案
尝试这个:
const batch = this.firestore.firestore.batch();
this.users.forEach(user => {
const insert = this.firestore
.collection('users')
.doc(user.id).ref; // Don't forget this ".ref"
batch.set(insert, user);
});
return batch.commit();