【问题标题】:How to batch add to collection in angular firestore?如何批量添加到 Angular Firestore 中的集合?
【发布时间】:2021-03-27 00:03:58
【问题描述】:

如何批量添加到 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()
  }

但是上面的逻辑是行不通的。

不知道为什么!

这个想法是创建一个集合(它在形式上不存在,但在旅途中创建),并在单个事务中推送整个用户列表,而不是单个发布请求。这可能吗?

想法是批量添加或更新集合

【问题讨论】:

  • 为什么不使用事务??
  • 无法引用集合。

标签: angular angularfire


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 2020-01-05
    • 2021-06-04
    • 1970-01-01
    • 2019-06-04
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多