如何在Meteor.methods中调用函数并返回值 [英] How to invoke a function in Meteor.methods and return the value

查看:153
本文介绍了如何在Meteor.methods中调用函数并返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了测试的目的,并保持简单,我得到一个值'b

Server.js

  Meteor.methods({
testingFunction:function(){
test()
}
});


函数test(){
var name ='test complete'
返回名称
}

client.js

  Template.profile.events ({
'click #button':function(event){
event.preventDefault();
Meteor.call('testingFunction',function(error,response){
如果(错误){
console.log(error);
} else {
console.log(response);
}
});
}
});


解决方案

语句将返回 undefined 。在这种情况下,您需要添加 return test()以将调用的值从您的方法返回给 test

  Meteor.methods({
testingFunction:function(){
return test();
}
});


Can you please tell me how I can invoke a function when I make a meteor method/call.

For test purposes and keeping it simple, I get a value of 'undefined' on the client console.

Server.js

Meteor.methods({
 testingFunction: function() {
   test()
  }
});


function test(){
 var name = 'test complete'
 return name
}

client.js

Template.profile.events({
'click #button': function (event) {
    event.preventDefault();
    Meteor.call('testingFunction', function(error, response) {
        if (error) {
            console.log(error);
        } else {
            console.log(response);
        }
    });    
}
});

解决方案

Any function without a return statement will return undefined. In this case, you need to add return test() to return the value of the call to test from your method.

Meteor.methods({
  testingFunction: function() {
    return test();
  }
});

这篇关于如何在Meteor.methods中调用函数并返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆