Meteor 向上投票和向下投票的职位-流星

Meteor 向上投票和向下投票的职位-流星,meteor,Meteor,向上和向下投票都是功能性的,但是我想做一个类似“如果用户是向下投票人还是向上投票人”的检查,然后做下面解释的正确的事情 upvote: function(postId) { check(this.userId, String); check(postId, String); var affected = Posts.update({ _id: postId, upvoters: {$ne: this

向上和向下投票都是功能性的,但是我想做一个类似“如果用户是向下投票人还是向上投票人”的检查,然后做下面解释的正确的事情

upvote: function(postId) {    
    check(this.userId, String);    
    check(postId, String);
    var affected = Posts.update({      
        _id: postId,       
        upvoters: {$ne: this.userId}
    },{ 
        $addToSet: {
            upvoters: this.userId
        },  
        $inc: {
            upvotes: 1
        }
    });

    if (! affected)      
        throw new Meteor.Error('invalid', "You already up-voted this post");
},

downvote: function(postId) {    
    check(this.userId, String);    
    check(postId, String);
    var affected = Posts.update({      
        _id: postId,       
        downvoters: {$ne: this.userId},
    }, {      
        $addToSet: {
            downvoters: this.userId
        },  
        $inc: {
            downvotes: 1
        }
    });

    if (! affected)      
        throw new Meteor.Error('invalid', "You already down-voted this post");     
},
使用我上面的代码,用户可以向上投票和向下投票一次,但他们可以同时做这两件事

我编写了一段代码,描述了当用户是向下投票人并单击upvote时会发生什么,但我不知道如何检查用户是向下投票人还是向上投票人

$pull: {
        downvoters: this.userId
    },
$addToSet: {
        upvoters: this.userId
    },  
    $inc: {
        downvotes: -1
    },
    $inc: {
        upvotes: 1
});
编辑:即使接受的答案很好,我还是发现了一个问题。当您单击“快速”时,投票计数可能会增加2-3倍。我没有增加投票计数,而是只插入userId,并简单地计算upvorters/downvorters数组中有多少id,这会给出相同的结果&它从不两次插入相同的userId

在计数的帮助程序中:

return this.upvoters.length
此外,inArray是一个有用的工具,用于检查您的值是否在数组中

if($.inArray(Meteor.userId(), this.upvoters)) //gives true if the current user's ID is inside the array

您必须获取帖子并查看其
下拉列表中是否包含用户id
数组:

var post = Posts.findOne(postId);
if (post.downvoters && _.contains(post.downvoters, this.userId)) {
  Posts.update({      
      _id: postId
    },
    {
      $pull: {
        downvoters: this.userId
      },
      $addToSet: {
        upvoters: this.userId
      },  
      $inc: {
        downvotes: -1,
        upvotes: 1
      }
    }
  });
}

谢谢!很有魅力