我有一个 React Web 应用程序,我正在尝试批量上传到 Firestore。我有一个上传按钮,可以触发下面的功能。第一次单击该按钮时,我在日志中看到第一条消息,但仅此而已 - batch.commit() 返回的 promise 既未解决也未拒绝,因此其他控制台消息均未出现。网络选项卡中没有任何内容。
当我第二次单击上传按钮时,数据就会上传,并且控制台中会出现完成消息。这是怎么回事?
const onUploadClick = () => {
const db = firebase.firestore()
const batch = db.batch()
const nycRef = db.collection('activities').doc('NYC')
batch.set(nycRef, { name: 'New York City' })
console.log('The batch is set to go.')
batch
.commit()
.then(function() {
console.log('Done.')
})
.catch(err => console.log(`There was an error: ${err}`))
}
最佳答案
我通过将 const db = firebase.firestore()
移动到另一个位置来使其工作,该位置将在应用程序启动时执行,而不是在上传发生之前执行。我在文档中没有看到任何说明需要这样做,所以我打开了 issue .
关于javascript - Firestore batch.commit() 第一次调用时不会将数据发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57702008/