Javascript Firebase:事务读取和更新多个文档

Javascript Firebase:事务读取和更新多个文档,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,使用此代码,我可以读取和更新事务中的单个文档 // Update likes in post var docRef = admin .firestore() .collection("posts") .doc(doc_id); let post = await admin.firestore().runTransaction(t => t.get(docRef)); if (!post.exists) { console.log("post not exist") } p

使用此代码,我可以读取和更新事务中的单个文档

// Update likes in post
var docRef = admin
  .firestore()
  .collection("posts")
  .doc(doc_id);

let post = await admin.firestore().runTransaction(t => t.get(docRef));
if (!post.exists) {
  console.log("post not exist")
}
postData = { ...post.data(), id: post.id };
let likes = postData.likes || 0;
var newLikes = likes + 1;
await post.ref.update({ likes: newLikes });
问题:
但是我需要阅读和更新多个文档,每个文档都会根据其内容进行更新。例如,我想像在我的代码中一样更新帖子集合中的赞数,但也要更新我的个人资料文档中的总赞数。

要更新事务中的多个文档,请多次调用
t.update()

let promise = await admin.firestore().runTransaction(transaction => {
  var post = transaction.get(docRef);
  var anotherPost = transaction.get(anotherDocRef);

  if (post.exists && anotherPost.exists) {
    var newLikes = (post.data().likes || 0) + 1;
    await transaction.update(docRef, { likes: newLikes });
    newLikes = (anotherPost.data().likes || 0) + 1;
    await transaction.update(anotherdocRef, { likes: newLikes });
  }
})

请参见

,但这里我们仅从一个文档中读取数据。我需要更新另一个docRef,这取决于他们的likes号码,而不是docRef likes号码。您可以调用
事务。获取(…)
事务中的多个文档引用。我从您的代码中复制了它。现已更新,以消除该问题的可能原因。但请注意,我试图表明您可以从事务处理程序中调用
transaction.get(…)
multiple来解决您的问题。发现错误:需要在“transaction=>”之前添加async,并将wait放在post和其他post上declaration@FrankvanPuffelenfirestore事务更新是否像批处理更新一样全部失败或成功?