我正在Firebase中构建一个实时投票.每个投票都存储在列表字段中.为了防止必须将每个投票都下拉到客户端以便对它们进行计数,我会在计数器字段中为每个选项缓存标签.
poll1
counts
choice1: 5
choice2: 2
choice3: 10
choice4: 252
voters
uid1 : choice1
uid6 : choice3
uid25: choice2
uid31: choice1
Run Code Online (Sandbox Code Playgroud)
我目前正在使用以下事务更新计数器:
var fireCountPush = new Firebase(self.fireCountUrl+node+id);
fireCountPush.transaction(function(current_value) {
return current_value + 1;
}, function(error, committed, snapshot) {
if(committed) {
var fireVote = new Firebase(self.fireVotesUrl);
fireVote.child(self.user.uid).set(id);
}
});
Run Code Online (Sandbox Code Playgroud)
但是,我想以原子方式将用户添加到选民列表中,理想情况是在同一事务中.不幸的是,我现在拥有它的方式,我必须在事务提交成功后添加用户.这是一个巨大的安全问题,因为可以通过编辑脚本在浏览器中轻松禁用它.
有什么办法既更新计数器,并添加用户到选民名单,而无需下载整个对象的交易?