我们如何在 firestore (RNFirestore) 的一次调用中创建一个文档,给它一些字段,并给它一个子集合?
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 中有一个集合,我向其中添加了一个文档,其中包含如下一些字段:
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(() => { });
});
我想在一次通话中完成这两项操作,但找不到任何线索。有什么办法吗?我试图通过减少承诺调用的数量来提高性能:)
无法在单个 API 调用中创建多个文档。写入单个文档始终需要单个 API 调用。
但是您可以通过使用 Firestore 所谓的 batched write 自动创建两个文档。虽然这仍然需要对每个文档进行一次 set
调用,但这些文档将作为一个操作发送到服务器(并在那里提交或拒绝)。
所以,我在 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(() => { });
});
我想在一次通话中完成这两项操作,但找不到任何线索。有什么办法吗?我试图通过减少承诺调用的数量来提高性能:)
无法在单个 API 调用中创建多个文档。写入单个文档始终需要单个 API 调用。
但是您可以通过使用 Firestore 所谓的 batched write 自动创建两个文档。虽然这仍然需要对每个文档进行一次 set
调用,但这些文档将作为一个操作发送到服务器(并在那里提交或拒绝)。