服务器代码中的Meteor 1.2.1版本Meteor.method({})不起作用

时间:2016-02-18 11:21:58

标签: javascript meteor npm

客户端点击sendlogmessage调用该事件,但服务器端程序内的方法不会调用sendlogmessage

console.log无效。任何人都可以帮我找到答案吗?

if (Meteor.isClient) {
    Template.mytemplate.events({
        "click": function () {
            Meteor.call('sendLogMessage');
        }
    })
}    

if (Meteor.isServer) {
    Meteor.methods({
        'sendLogMessage': function(){
            console.log("Hello world");
        }
    });
}

1 个答案:

答案 0 :(得分:0)

你的代码正在运作。您只需指定要单击的元素即可触发事件。像这样:

HTML
<template name="mytemplate">
    <p id="clicable">Click me!</p>
</template>

JAVASCRIPT
Template.mytemplate.events({
    "click #clicable": function() {
        Meteor.call('sendLogMessage');
    }
})

HTML
<template name="mytemplate">
    <p class="clicable">Click me!</p>
</template>

JAVASCRIPT
Template.mytemplate.events({
    "click .clicable": function() {
        Meteor.call('sendLogMessage');
    }
})