firebase - 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;
})
})
})
我感谢所有的反馈!干杯。
解决方案
如果您执行以下操作会发生什么:
return followerCount.transaction((currentCount) => {
return currentCount || 0 + 1;
})
.then((count) => {
if (count.committed) {
return followingCount.transaction((currentCount) => {
return currentCount || 0 + 1;
})
}
})
推荐阅读
- xamarin - 无法两次点击相同的列表视图项目xamarin
- django - Django:inline_formset 的更新表单不起作用
- c# - 如何模拟 LocalView
- java - 在 JBoss EAP 7.1 中加载不同的 EL 版本
- sql - Apache Calcite 仅在使用带有无类型空值的 COALESCE 时引发错误
- json - 蜂巢:解析 JSON 包含 \"
- amazon-web-services - 如何使 AWS REST API 返回 JSON 而不是 XML
- python - 删除可为空的字段正在删除相关对象
- system-verilog - 如何在 SystemVerilog 规范中标记问题
- apache-kafka - 将 Schema Registry 与 Kafka 和 Avro 一起使用时,每个主题有一个或多个模式......?