【问题标题】:Meteor Methods and CoffeeScriptMeteor 方法和 CoffeeScript
【发布时间】:2014-11-28 04:57:36
【问题描述】:

由于某种原因,我无法让服务器上的方法执行。我在服务器上没有看到“计时器已启动”console.log,并且“foo”没有返回。我错过了什么?

请看代码:

if Meteor.isClient
    console.log "client is alive and well."
    Session.setDefault("duration", 1200000)
    Session.setDefault("timeRemaining", 1200000)

    Template.timer.helpers
        duration: () ->
            Session.get("duration")

        timeRemaining: () ->
            Session.get("timeRemaining")

    Template.timer.events
        'click #start': () ->
            console.log "Started."
            Meteor.call("startTimer", (error, result) ->
                console.log "Result is #{result}")  

        'click #pause': () ->
            console.log "Paused."
            Meteor.call("pauseTimer", (error, result) ->
                console.log "Result is #{result}")  

        'click #cancel': () ->
            console.log "Cancelled."
            Meteor.call("cancelTimer", (error, result) ->
                console.log "Result is #{result}")      

if Meteor.isServer
    Meteor.startup () ->
        console.log "Server is alive."

    Meteor.methods
        startTimer: () ->
            console.log "Timer has started."
            "foo"

【问题讨论】:

  • 如果你在浏览器控制台输入 Meteor.call("startTimer") 会发生什么
  • 嗨@KeithNicholas,它返回为“未定义”。
  • 这很正常。它在你的服务器控制台上说什么?
  • 除了“服务器处于活动状态”之外,服务器控制台上没有显示任何内容。

标签: methods meteor coffeescript


【解决方案1】:

看起来不错。您确定在名为 timer 的模板中有您的 id 为“start”的元素吗?

我将您的代码复制并粘贴到具有最少 HTML 的 Meteorpad 中,它可以工作:

http://meteorpad.com/pad/d9KLWPj6SZPDxr4Et/SO-Answer

单击测试按钮,您应该会看到计时器已在黑色控制台中启动(从服务器输出),如果您检查浏览器的控制台,结果为 foo

您甚至在浏览器控制台中登录“开始”了吗?如果没有,那么您的事件没有正确连接。

【讨论】:

  • 我不知道出了什么问题。我从 Meteorpad 拿了你的代码,它工作了。谢谢!
猜你喜欢
  • 2014-02-16
  • 2013-03-27
  • 2018-11-18
  • 2012-02-23
  • 1970-01-01
  • 1970-01-01
  • 2014-07-13
  • 2013-09-21
  • 2014-10-28
相关资源
最近更新 更多