如何在反应还原数据库中编写火库批



在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-firebaseredux-firestore分别扩展了firebasefirestore的原始实现。

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>
}

最新更新