【问题标题】:Meteor- User can only upvote or downvote onceMeteor - 用户只能投赞成票或反对票一次
【发布时间】:2016-09-14 07:50:31
【问题描述】:

我对我的流星应用程序进行了支持和反对,但用户可以根据需要进行支持和反对。我正在努力做到这一点,所以他们只能做一个,如果他们做另一个,它会删除前一个。

我的 Autoform 如下所示:

ArticleSchema  = new SimpleSchema({
  title: {
    type: String,
    label: "Article title",
    autoform: {
      placeholder: "full article title here"
    }
  },
  url:{
    type: String,
    label:  "URL",
    autoform: {
      placeholder: "http://website.com"
    }
  },
  username:{
    type: String,
    label: "Author",
    autoValue: function() {
      return Meteor.user().username
  }
},
  author:{
    type: String,
    label: "Author",
    autoValue: function() {
      return this.userId
  },
  autoform: {
    type: "hidden"
  }
},
  autoform: {
    type: "hidden"
  }
  },
  createdAt: {
    type: Date,
    label: "Created At",
    autoValue: function(){
      return new Date()
    },
    autoform: {
      type: "hidden"
    }
  },
  upvote: {
    type:Number,
    optional:true
  },
  upVoters:{
    type:Array,
    optional:true
  },
  'upVoters.$':{
    type:String
  },
  downvote: {
    type:Number,
    optional:true
  },
  downVoters:{
    type:Array,
    optional:true
  },
  'downVoters.$':{
    type:String
  },
});

我的活动:

Template.articleOutput.events({
  "click .upvote": function(){
    var articles = Articles.findOne({ _id: this._id });
    if( articles ) {
      if (_.contains(articles.downVoters, this.userId)) {
        Articles.update({
             _id: this._id
          },
          {
            $pull: {
              downVoters: this.userId
            },
            $addToSet: {
              upVoters: this.userId
            },
            $inc: {
              downvote: -1,
              upvote: 1
            }
          }
        );
      }
      Articles.update(this._id, {$addToSet: {upVoters: Meteor.userId()}, $inc: {upvote: 1}});
    }
  },

  "click .downvote": function(){
    var articles = Articles.findOne({ _id: this._id });
    if( articles ) {
      if (_.contains(articles.upVoters, this.userId)) {
        Articles.update({
             _id: this._id
          },
          {
            $pull: {
              upVoters: this.userId
            },
            $addToSet: {
              downVoters: this.userId
            },
            $inc: {
              downvote: 1,
              upvote: -1
            }
          }
        );
      }
      Articles.update(this._id, {$addToSet: {downVoters: Meteor.userId()}, $inc: {downvote: 1}});
    }
  }
});

还有 HTML:

<template name="articleOutput">
    <div class="outputBoxHome" >
      <div class="vote">
        <div class="upvote">

        </div>
        <div class="downvote">

        </div>
      </div>
      <div class="outputBoxHomeContent">
        <a href="/articles/{{_id}}"><h4>{{title}}</h4></a>
      </div>
    </div>
</template>

使用此代码,用户可以进行投票,并将其连同他们的姓名一起添加到 upVoter,反之亦然

编辑

我通过使用 $.inArray 而不是 _.contains 来修复它

if ($.inArray(articles.upVoters, this.userId)) {

【问题讨论】:

    标签: javascript meteor meteor-autoform


    【解决方案1】:

    我已通过使用 $.inArray 而不是 _.contains 来修复它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-07
      • 2018-07-08
      • 2012-03-15
      • 2010-10-06
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 2010-12-04
      相关资源
      最近更新 更多