【问题标题】:Exception while simulating the effect of invoking 'upvote'. TypeError: undefined is not a function模拟调用“upvote”的效果时出现异常。类型错误:未定义不是函数
【发布时间】:2014-10-12 20:18:10
【问题描述】:

尝试将“upvoting”添加到Crowducate.me 的特定课程中。

从图片中可以看出,该方法已正确调用。但是,我认为更新方法(mongo)是不理解的。这是我的个人源代码all commits on Github.

My courses.coffee (mongo collection) // btw: can't format proper indentation here:

class @Course extends Minimongoid
@_collection: new Meteor.Collection('courses') 
...
Meteor.methods({
  createCourse: ->
    userId = Meteor.userId()
    throw new Meteor.Error 403, 'Please login to create a course' unless userId
    title = 'New Course'
    course = Course.create {
    owner: userId, courseTitle: title, published: 0, upvoters: [], votes: 0, slug: slugify(title)
  }
  return course._id

  upvote: (courseId) ->
    course = Course.first({_id: courseId})
    console.log course

  #userId = Meteor.user()
    Course.update {
      _id: courseId,
      # upvoters: {$ne: user._id}
      # }, {
      # $addToSet: {upvoters: user._id}
      $inc: {votes: 1}
    } 

还有我的html

<template name="courseListItem">
  <div class="course-list-item">
    <a href="{{#if isMyCoursesPath}}{{pathFor 'courseUpdate'}}{{else}}{{pathFor 'courseShow'}}{{/if}}">
      <div class="course-img">
        {{#if courseOwner}}
        <div class="owner-options">
          <a href="{{pathFor 'courseUpdate'}}" class="btn btn-default" title="Edit Course"><i class="fa fa-edit"></i></a>
          <button class="btn btn-danger delete" title="Delete Course"><i class="fa fa-trash-o"></i></button>
        </div>
        {{/if}}
        <img class="img-resonsive" src="{{courseThumb}}">
      </div>
      <div class="well relative">
        <div class="course"><h4>{{maxChars courseTitle 40}} by {{owner}}</h4></div>
        <p class="course-list-item-preview-text">{{maxChars getText 65}}</p>
        <p class="course-list-item-preview-text-votes"><a href="#" class="btn btn-default btn-xs"><i class="upvote fa fa-thumbs-up"></i></a> <strong>{{votes}}</strong> Votes</p>
        <!-- <p class="course-list-item-preview-text">12 Votes</p> -->
        <div class="bottom">
          <p><i class="fa fa-tags" title="Tags"></i> {{maxChars getKeywords 28}}</p>
          <p><i class="fa fa-group" title="Age Group"></i> {{age}}</p>
        </div>
      </div>
    </a>
  </div>
</template>

我的助手:

'click .upvote': (evt) ->
    Etc.prevent(evt)
    Meteor.call 'upvote', this._id

也许这是特定的规则。到 Coffeecode 或 MinimongoId?如果您想在本地运行它,请使用 mrt install 并且不要更新到包或流星。另外,您测试赞成投票,您需要快速创建课程(在“教学”部分下)。

最好, 阿米尔 P.S.:只是让您知道:我正在扩展另一个人的代码,并且对 CoffeeScript 没有太多经验。

【问题讨论】:

  • 请修正你的 CoffeeScript 中的缩进,这很重要。添加代码,选择它,然后点击{ } 图标。
  • Course.first 应该是Course.findOne
  • @muistooshort : 完成
  • @Peepe L-G:这在某种程度上被设置为相当于 Course.findOne。这就是 console.log 起作用的原因

标签: javascript mongodb coffeescript meteor


【解决方案1】:

问题必须出在方法中:upvote 并且您在那里调用了一个不存在的函数。

即使该方法看起来不错,也请尝试取消注释任何内容,而不是简单的 console.log 'hi'。并逐行删除uncmets。

或者可能是您使用的 minimongoid 软件包版本没有更新。尝试 Course._collection.update(...) 代替。

【讨论】:

    猜你喜欢
    • 2016-09-23
    • 2017-09-27
    • 2015-11-13
    • 2015-06-17
    • 2015-10-25
    • 2011-06-16
    • 1970-01-01
    • 2021-10-25
    • 2014-05-26
    相关资源
    最近更新 更多