【问题标题】:How do we create a document, give it some fields, and give it a sub collection all in a single call in firestore (RNFirestore)?我们如何在 firestore (RNFirestore) 的一次调用中创建一个文档,给它一些字段,并给它一个子集合?
【发布时间】:2021-02-26 00:52:52
【问题描述】:

所以,我在 firestore 中有一个集合,我向其中添加了一个包含如下字段的文档:

const chatRef = firestore().collection('CHAT').doc(id);

    chatRef.set({
        field-1: 'something',
        field-2: 'something',
      })
      .then(() => { });

现在,要向该文档添加子集合,我将以下代码添加到“then”。完整的代码如下所示:

    const chatRef = firestore().collection('CHAT').doc(id);

    chatRef.set({
        field-1: 'something',
        field-2: 'something',
      })
      .then(() => {
            chatRef.collection('MESSAGES').add(initialBotMessage)
              .then(() => { });
      });

我想在一次通话中完成这两项操作,但找不到任何潜在客户。有没有办法做到这一点?我试图通过减少承诺调用的数量来提高性能:)

【问题讨论】:

    标签: javascript firebase google-cloud-firestore react-native-firebase


    【解决方案1】:

    无法在一次 API 调用中创建多个文档。写入单个文档始终需要单个 API 调用。

    但是您可以通过使用 Firestore 所称的 batched write 以原子方式创建这两个文档。虽然这仍然需要为每个文档调用一次set,但这些将作为单个操作发送到服务器(并在那里提交或拒绝)。

    【讨论】:

      猜你喜欢
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 2018-08-08
      相关资源
      最近更新 更多