首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在react-redux-firebase中编写firestore批处理

如何在react-redux-firebase中编写firestore批处理
EN

Stack Overflow用户
提问于 2020-03-18 01:39:20
回答 1查看 276关注 0票数 0

在react-redux-firebase中,如何进行类似于firestore文档中所示的批处理写入?(见下文)

代码语言:javascript
复制
// 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 () {
    // ...
});
EN

回答 1

Stack Overflow用户

发布于 2020-09-10 16:30:52

像这样的东西会起作用的。不要忘记,react-redux-firebaseredux-firestore分别扩展了firebasefirestore的原始实现。

代码语言:javascript
复制
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>
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60727502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档