2016-10-13 164 views
0

在我的Meteor應用程序中,這個帶有方法調用的投票功能工作正常,直到我包含一個輸出lodash的compability腳本爲var _ = runInContext();現在我得到的錯誤是_.contains不是函數。不用lodash調用方法的另一種方法是什麼?

是否有另一種方式來運行此功能,並呼籲沒有_.contains

在按鈕點擊:

"click [data-action='addLikes']": function (event) { 
    event.preventDefault(); 
    var song = Songs.findOne({_id: this._id}); 
    upvote(song); 
} 

服務器方法:

upvote = function(currentSong){ 
    var user = Meteor.user(); 
    if(!user){ 
    return false; 
    } 
    if (currentSong) { 
    if (_.contains(currentSong.voters, Meteor.userId())) { 
     return false; 
    } 
    Songs.update(currentSong._id, {$addToSet: {voters: Meteor.userId()}, $inc: {likes: 1}}); 
    } 
}; 

回答

0

如果currentSong.voters只是一個數組,你可以用兩種解決方案去:

ES6

currentSong.voters.includes(Meteor.userId()) 

ES5

currentSong.voters.indexOf(Meteor.userId()) > -1 

或簡寫

~currentSong.voters.indexOf(Meteor.userId()) 
+0

哦,更容易比我想象。按照描述工作,謝謝! –

相關問題