我想實現一個安全的投票系統,不能用客戶端控制檯中的Meteor.call()進行更改。如果他們登錄,每個人都可以上傳和下載一些帖子,但每個用戶和帖子只需要一次。製作投票系統安全Meteor.Methods
ONY我的客戶端Ive得到了這樣的事情:
Template.postArgument.events({
'click .yes':function() {
if(Meteor.user()) {
var postId = Arguments.findOne({_id:this._id})
var currentArgumentId = this._id;
if($.inArray(Meteor.userId(), postId.votedUp) ===-1) {
if($.inArray(Meteor.userId(), postId.votedDown) !==-1) {
Meteor.call('argumentVoteYes',currentArgumentId);
} else {
Meteor.call('argumentVoteYesElse',currentArgumentId);
}
} else {
Meteor.call('argumentVoteYesElseElse',currentArgumentId);
}
}
}}
)};
在我的服務器:
Meteor.methods({
'argumentVoteYes':function(currentArgumentId){
Arguments.update(currentArgumentId, {
$pull: {votedDown: Meteor.userId()},
$inc: {score: 2 },
$addToSet: {votedUp: Meteor.userId() }
});
},
'argumentVoteYesElse':function(currentArgumentId){
Arguments.update(currentArgumentId, {
$inc: {score: 1 },
$addToSet: {votedUp: Meteor.userId() }
});
},
'argumentVoteYesElseElse':function(currentArgumentId){
Arguments.update(currentArgumentId, {
$inc: {score: -1 },
$pull: {votedUp: Meteor.userId()}
});
}
'argumentVoteNo':function(currentArgumentId){
Arguments.update(currentArgumentId, {
$pull: {votedUp: Meteor.userId()},
$inc: {score: -2 },
$addToSet: {votedDown: Meteor.userId() },
});
},
'argumentVoteNoElse':function(currentArgumentId){
Arguments.update(currentArgumentId, {
$inc: {score: -1 },
$addToSet: {votedDown: Meteor.userId() },
});
},
'argumentVoteNoElseElse':function(currentArgumentId){
Arguments.update(currentArgumentId, {
$inc: {score: 1 },
$pull: {votedDown: Meteor.userId()}
});
},
});
的問題是如何獲得這個安全的,例如,如果有人撥打一個Meteor.call('argumentvoteYes', "someID" , {$inc: {score:2}});
它會增加2的分數。如果用戶調用這個兩次,投票將增加4.有沒有辦法以一種安全的方式做到這一點?
嘿米歇爾!我嘗試了你的建議,如果我使用'userId = Meteor.userId();'爲什麼它沒有與'var userId = this.userId();'合作?感謝您的幫助! – decisionMaker
如果我嘗試爲我的第二個調用'argumentVoteYesElse'實現相同的邏輯,我得到一個'無法調用方法'indexOf'未定義的錯誤。猜猜它的原因,我沒有插入votedUp時創建一個新的職位??。當有人點擊新帖子的第一次,並希望投票發佈此帖時,錯誤消息發生。所以在嘗試post.votedUp.indexOf(userId)時沒有voteedUp。我現在該做什麼?我已經爲帖子添加了投票向下按鈕。如果在voteDown上單擊第一次,然後更改爲voteUp,則會出現同樣的問題。 – decisionMaker
查看對代碼的更改 –