Filip Blaauw Filip Blaauw - 1 month ago 11
Javascript Question

Another way to call method without lodash?

In my Meteor app, this voting function with a method call was working fine, until I included a compability script that exports lodash as

var _ = runInContext();
Now I get error that
_.contains
is not a function.

Is there another way to run this function and call without
_.contains
?

On button click:

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


Server method:

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}});
}
};

Answer

If currentSong.voters is just an array, you can go with two solutions:

ES6:

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

ES5:

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

or a shorthand

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