【问题标题】:Meteor: calling function from Meteor.methodsMeteor:从 Meteor.methods 调用函数
【发布时间】:2013-08-01 21:08:20
【问题描述】:

首先我想注意到我是 Meteor 的初学者。我不知道为什么这个代码:

Meteor.methods =
  fun: ->
    "This is message."

if Meteor.isClient
  Template.hello.greeting = ->
    "Welcome to FirstApp."

  Template.hello.events =
    "click input": ->
       console.log "You pressed the button."

当此行在浏览器控制台中输入时:

Meteor.call("fun", function(err, res) { if(err) alert(err); else alert(res); });

警报:错误:找不到方法 [404] 而不是“这是消息。”。 为什么乐趣是不确定的?

【问题讨论】:

    标签: methods meteor call


    【解决方案1】:

    您是否将此文件放在client 目录中? Meteor.methods 必须在服务器上定义 fun 才能正确触发回调。

    Meteor.methods 的客户端版本只定义了一个本地存根。

    【讨论】:

      【解决方案2】:

      因为不应将Meteor.methods 分配给函数。如果你这样做,你将永远只有一个功能。

      试试这个:

      if Meteor.isServer
         Meteor.methods({
            fun: ->
               "This is message."
         })
      

      我不知道coffeescript 可以在客户端上使用。但是你的定义是错误的,因为事件是一个具有多种功能的对象

      这是正确的(假设咖啡脚本在客户端上工作):

      Template.hello.events({
         "click input": ->
            console.log "You pressed the button."
      })
      

      玩 Meteor 并阅读 documentation

      【讨论】:

        猜你喜欢
        • 2012-05-07
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 2016-03-09
        • 2015-09-14
        • 1970-01-01
        • 2013-05-08
        • 1970-01-01
        相关资源
        最近更新 更多