Angular 批量更新不工作到firebase

xriantvc  于 7个月前  发布在  Angular
关注(0)|答案(1)|浏览(82)

我创建了一个多维数组newOrder

0: {id: 'JNuxJewf4E8YyX7t3fPk', order: 2000}
1: {id: 's2kCZnF5vhp1E6cW3VXI', order: 3000}
2: {id: 'UGPPNa7zTuUkBmuorF2v', order: 4000}
3: {id: '0YzyArRPQQ3ZyAOfOJjM', order: 5000}
4: {id: 'LpZnzX6NufWsaVQ52lyS', order: 6000}
5: {id: 'VfL8pX77LCxXniWQunk7', order: 7000}
6: {id: '8QtF3CbpVjIXeI4ziHxy', order: 8000}
7: {id: 'y05y6Hta6kesXymg034Q', order: 9000}
8: {id: 'LQPq6q3Dp6CeACTiO8IC', order: 10000}
9: {id: 'bmeT3h3kG7Oeilf0nIQ8', order: 11000}
10:{id: 'mrHFK2wEsn9UhFz5AOEQ', order: 12000}

字符串
我试着遍历它们,并使用id:更新每条记录中的order:字段。到目前为止,我已经了解到批量更新是一种方法。所以我写了以下代码:

const batch = this.fireStore.firestore.batch();
this.newOrder.forEach((val, index) => {
  const id = this.newOrder[index]['id'];
  const orderNum = this.newOrder[index]['order'];
  console.log (' id:' + id + ' order:' + orderNum);
  const pageRef = this.fireStore.doc('content/${id}').ref;
  batch.update(pageRef, {order:orderNum});
});


我没有收到任何错误,控制台日志输出正确,但它没有更新firebase记录。我做错了什么?我已经在这2天了。

qvtsj1bj

qvtsj1bj1#

从你的代码来看,你似乎错过了对commit()的调用。这个异步方法返回一个Promise,一旦批处理中的所有写操作都作为一个原子单元成功写入后端,它就被解析。

const batch = this.fireStore.firestore.batch();
this.newOrder.forEach((val, index) => {
  const id = this.newOrder[index]['id'];
  const orderNum = this.newOrder[index]['order'];
  console.log (' id:' + id + ' order:' + orderNum);
  const pageRef = this.fireStore.doc(`content/${id}`).ref;
  batch.update(pageRef, {order:orderNum});
});
batch.commit()
.then( => {  // ... If necessary  });

字符串

相关问题