首页 > 解决方案 > Firebase 云在一个功能中处理多个事务?

问题描述

我正在尝试创建一个同时执行两个事务的云函数。

我尝试执行以下操作,仅执行第一个事务......当我添加第二个事务时它不起作用并且我没有收到任何错误。

功能:

exports.followIncrement = functions.database
.ref("/followers/{userId}").onCreate((snapshot, context) => {
  const userId = context.params.userId;
  const currentUserUid = Object.keys(snapshot.val())[0];

  console.log(currentUserUid);
  console.log(userId);

  var followerCount = 
admin.database().ref('users').child(userId).child('followerCount')
  var followingCount = 
admin.database().ref('users').child(currentUserUid).child('followingCount')

return followerCount.transaction((currentCount) => {
  return currentCount || 0 + 1;
})
.then(() => {
  return followingCount.transaction((currentCount) => {
      return currentCount || 0 + 1;
    })
  })
})

我感谢所有的反馈!干杯。

标签: firebasegoogle-cloud-functions

解决方案


如果您执行以下操作会发生什么:

return followerCount.transaction((currentCount) => {
  return currentCount || 0 + 1;
})
.then((count) => {
  if (count.committed) {
    return followingCount.transaction((currentCount) => {
      return currentCount || 0 + 1;
    })
  }
})

推荐阅读