简体   繁体   English

如何批量添加到 angular firestore 中的集合?

[英]How to batch add to collection in angular firestore?

How to batch add to collection in angular firestore?如何批量添加到 angular firestore 中的集合?

Following function tries to create new users inside a collection:以下 function 尝试在集合中创建新用户:

  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()
  }

But the above logic is not working.但是上面的逻辑是行不通的。

Not sure why!不知道为什么!

The idea was to create a collection (which formally doesnot exists but created on the go), and push the whole list of users in a single transaction, instead of single post requests.这个想法是创建一个集合(它正式不存在但在旅途中创建),并在单个事务中推送整个用户列表,而不是单个发布请求。 Is that possible?那可能吗?

The idea was to bulk add or update a collection这个想法是批量添加或更新集合

Try this:尝试这个:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM