react-redux-firebase - 如何在 react-redux-firebase 中编写 firestore 批处理
问题描述
在 react-redux-firebase 中,如何进行类似于 firestore 文档中显示的批量写入?(见下文)
// Get a new write batch
var batch = db.batch();
// Set the value of 'NYC'
var nycRef = db.collection("cities").doc("NYC");
batch.set(nycRef, {name: "New York City"});
// Update the population of 'SF'
var sfRef = db.collection("cities").doc("SF");
batch.update(sfRef, {"population": 1000000});
// Delete the city 'LA'
var laRef = db.collection("cities").doc("LA");
batch.delete(laRef);
// Commit the batch
batch.commit().then(function () {
// ...
});
解决方案
像这样的东西会起作用。不要忘记这一点,react-redux-firebase
并分别扩展和redux-firestore
的原始实现。firebase
firestore
const Counter = () => {
const firestore = useFirestore()
const batch = firestore.batch()
const nycRef = firestore.get({collection: 'cities', doc: 'NYC'})
batch.set(nycRef, {name: 'New York City'})
const sfRef = firestore.get({collection: 'cities', doc: 'SF'})
batch.update(sfRef, {population: 10000000})
const laRef = firestore.get({collection: 'cities', doc: 'LA'})
firestore.delete(laRef)
const runBatch = async () => await batch.commit()
return <button onClick={runBatch}>Attempt Batch</button>
}
推荐阅读
- php - 使用 memcache 和 php 7 进行会话故障转移
- javascript - 为什么 asm.js 比普通 js 慢(生成素数)?如何加快速度?
- javascript - Vis js - 避免边缘重叠
- python - 如何在十六进制 Linux 终端中发送参数
- c++ - 从不同线程推送到队列时出现分段错误
- bootstrap-4 - 升级到引导程序 4 后,可选项卡窗格在 IE 11 中停止工作
- google-maps - 根据坐标创建 Google 地图链接 - 错误引脚位置
- flutter - Flutter:从子 StreamBuilder 更新有状态小部件
- azure - 如何计算 Monitor - Cosmos DB(预览版)请求?
- javascript - 显示从开始日期到结束日期的周数