我创建了一个脚本,该脚本通过firebase执行请求。由于我想避免同时在一个关系上进行多个查询,因此我使用了代码中所示的事务处理功能。首先,获取关系的当前状态,并检查其是否存在。这似乎有效。第一次是常量current = null
,将执行向data2
添加值的函数。
而且,如果我第二次运行脚本,那么他将成功进入下一个功能(doSecondFunction()
)。
不幸的是,这里没有currentData返回给我。我希望第一次添加的引用data2
的值。但是结果是我得到了null
。
有没有一种方法可以处理数据库当前状态的事务?
const ref = firebase.database().ref('data2');
ref.once('value', function(snapshot) {
const current = snapshot.val();
if(current == null){
console.log('FIRST');
addValToRefFunction();
}else{
console.log('SECOND');
doSecondFunction(current, ref);
}
});
function doSecondFunction(current, ref){
ref.transaction(function(currentData){
console.log(current); // result is the right value
console.log(currentData); // result is null ?
if (currentData === current){
console.log('Check');
return;
}
}
答案 0 :(得分:0)
在开始事务时,无法控制客户端将其视为节点的当前状态。以我的经验,第一次调用回调时,该值几乎始终为null
,并且回调需要处理该值。
也可以在此处查看我的答案,以获取有关该过程的说明:Firebase runTransaction not working - MutableData is null