How To Invoke A Function In Meteor.methods And Return The Value
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. Se
Solution 1:
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();
}
});
Solution 2:
Here is a great example:
Client Side:
Baca Juga
- Js: How May I Access A Variable Value Inside A Function That's Inside Another Function, And That Both Functions Belong To The Same Object?
- Property Is Faster Than Method? Need Reason For It
- Event Handler Function In Prototype's Method, Why Does It Think .keycode Is A Property Of Undefined In Javascript?
// this could be called from any where on the client side
Meteor.call('myServerMethod', myVar, function (error, result) {
console.log("myServerMethod callback...");
console.log("error: ", error);
console.log("result: ", result);
if(error){
alert(error);
}
if(result){
// do something
}
});
Server Side:
// use Futures for threaded callbacks
Future = Npm.require('fibers/future');
Meteor.methods({
myServerMethod: function(myVar){
console.log("myServerMethod called...");
console.log("myVar: " + myVar);
// new future
var future = new Future();
// this example calls a remote API and returns
// the response using the Future created above
var url = process.env.SERVICE_URL + "/some_path";
console.log("url: " + url);
HTTP.get(url, {//other params as a hash},
function (error, result) {
// console.log("error: ", error);
// console.log("result: ", result);
if (!error) {
future.return(result);
} else {
future.return(error);
}
}
);
return future.wait();
}//,
// other server methods
});
Post a Comment for "How To Invoke A Function In Meteor.methods And Return The Value"