【发布时间】:2021-12-29 08:08:46
【问题描述】:
我想要一个可以使用事务递增或递减的计数器。我不想将计数器初始化为 0,而是它在开始时不存在。所以我会写如下的增量事务
ref.transaction(function(counterValue){
if (counterValue == null) return 1
else return (counterValue +1)
},function(error,committed,snapshot){
if (error){//fail
}else if (!committed){//aborted
}else{//successfull
}
})
但我不确定事务是否会正确运行,因为 counterValue 的 null 值可能意味着没有现有节点,而且当本地缓存为空时也是如此。 谁能告诉我我应该怎么做?
【问题讨论】:
标签: javascript firebase-realtime-database transactions