草庐IT

javascript - 模拟调用 'upvote' 的效果时出现异常。类型错误 : undefined is not a function

coder 2023-10-31 原文

尝试为 Crowducate.me 的特定类(class)添加“upvoting”。

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

我的 courses.coffee(mongo 集合)//顺便说一句:无法在此处设置正确的缩进格式:

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 并且不要更新包或 meteor。另外,您测试了投票,您需要快速创建一个类(class)(在“教学”部分下)。

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

最佳答案

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

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

或者可能是你使用的minimongoid包版本没有更新。请尝试使用 Course._collection.update(...)。

关于javascript - 模拟调用 'upvote' 的效果时出现异常。类型错误 : undefined is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25388369/

有关javascript - 模拟调用 'upvote' 的效果时出现异常。类型错误 : undefined is not a function的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  4. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  5. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  6. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  7. ruby - 如何模拟 Net::HTTP::Post? - 2

    是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou

  8. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  9. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  10. ruby - Infinity 和 NaN 的类型是什么? - 2

    我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串

随机推荐