【问题标题】:Find out which firestore batch action failed and what data was written to db?找出哪个 firestore 批处理操作失败以及将哪些数据写入 db?
【发布时间】:2018-11-22 10:56:51
【问题描述】:

我有一个函数来创建一个角色,该角色有 2 次写入 firestore 以创建角色并创建用户名(我创建用户名然后检查它是否已经存在,即强制使用唯一的用户名)。

try {
  const characterPrivateRef = firestore().doc(`characters/${uid}`);
  const usernameRef = firestore().doc(`usernames/${username}`);
  firestore().batch().batch.create(characterRef, {...characterData});
  firestore().batch().batch.create(characterPrivateRef, {...usernameData});

  await firestore().batch().commit();
  // How can I get data written to each firestore document from batch 
  here?
} catch (e) {

  // How can I check what batch failed here i.e. if it was username I 
  want to show error saying something like ("Username already exists")
}

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    如果您使用的是批量写入,则意味着您尝试将数据写入 Firestore 数据库原子,这意味着要么所有操作成功,要么所有操作失败。据我所知,没有办法知道批处理中这两个操作中的哪一个失败了。相反,您可以做的是附加一个完整的侦听器并在批处理操作失败后获取错误消息。如果你想知道哪个操作失败了,你应该把数据分开写,作为两个不同的写操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多