下面是我被绊倒的原因:
+2业力。+2业力。-2业力。(净额0)-2的业力。+2的业力。-2 (如果是向上投票)和+2 (如果是否决)。(净额0)我不知道这是否有意义,但本质上我想奖励那些投了一票的人,并惩罚那些创造糟糕职位的人。
以下是我所拥有的:
在我的数据库中,当一个帖子获得向上投票时,this.userVote是1,反对票是-1,而删除他们的选票则是0。
upvote() {
let vote = this.userVote == 1 ? 0 : 1;
this.database.list('/upvotes/'+this.postData.id).set(this.userData.uid, vote)
}
downvote() {
let vote = this.userVote == -1 ? 0 : -1;
this.database.list('/upvotes/'+this.postData.id).set(this.userData.uid, vote)
}因此,正如我前面所描述的,它的工作方式与预期的一样。
问题是,我不知道如何设置业力,以使它按预期工作。
我目前正在做以下行来更新用户业力以及海报业力:
this.database.database.ref('users/'+this.userData.uid).update({'karma': this.userKarma + karma}) //this.userKarma is the users karma
this.database.database.ref('users/'+this.postData.uid).update({'karma': this.postKarma + karma}) //this.postKarma is the karma of the user who created the post我应该如何设置变量karma,就像我前面提到的那样。
有什么建议吗?谢谢!
发布于 2018-04-23 00:20:22
最后我解决了这个问题:
getKarmaDelta(prevVote, vote) {
if((prevVote !== 1 && prevVote !== -1) && (vote === 1 || vote === -1)) {
this.updateUserKarma(2)
} else if (vote === 0) {
this.updateUserKarma(-2)
}
if(vote === -1 || (vote === 0 && prevVote === 1)) {
this.updatePosterKarma(-2)
} else if (vote === 1 || (vote === 0 && prevVote === -1)) {
this.updatePosterKarma(2)
}
}希望这能帮到别人。
发布于 2018-04-18 16:00:23
你可以用火柴交易系统。事务是原子化更新数据的一种方式(在firebase中没有真正的原子性)。
例如,将业力减少1。
this.database.database.ref('users/'+this.userData.uid).child('karma')
.transaction(function(karma){
if (!karma)
return 0;
return karma - 1;
});https://stackoverflow.com/questions/49904031
复制相似问题