Skip to content Skip to sidebar Skip to footer

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() {
    returntest();
  }
});

Solution 2:

Here is a great example:

Client Side:

// 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 callbacksFuture = Npm.require('fibers/future');

Meteor.methods({

    myServerMethod: function(myVar){

        console.log("myServerMethod called...");
        console.log("myVar: " + myVar);

        // new futurevar future = newFuture();

        // this example calls a remote API and returns// the response using the Future created abovevar 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"