0

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);
        }
    });    
}
});
meteorBuzz
  • 3,110
  • 5
  • 33
  • 60

2 Answers2

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();
  }
});
David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • Okay, I undertand. So although I 'return' a value in my test function, I must 'return' the function that I call. I did this and it works. Thank you – meteorBuzz Feb 16 '15 at 19:47
  • 1
    Not to be pedantic, but you are returning the result returned by the call to `test`. You are not returning the function `test`. It's the same as doing: `var result = test(); return result;` – David Weldon Feb 16 '15 at 19:51
  • I see. For example purposes, if I had complex function and I ended with a value in a variable 'newValue', do I always have to 'return' that value just like my test function? Or is there another way for this exact same use case? – meteorBuzz Feb 16 '15 at 20:01
  • 1
    In JavaScript you have to type `return` any time you need a value returned from a function. CoffeeScript, on the other hand, has implicit returns (automatically returns the result of the last expression). – David Weldon Feb 16 '15 at 20:05
  • Brilliant, I will read further into this David, thanks again! – meteorBuzz Feb 16 '15 at 20:13
1

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 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

});
Jason Cochran
  • 316
  • 3
  • 10
  • thanks for showing the use of 'future.' I have used it within my actual function. However, I simply had trouble calling a function outside of my method and have the value returned. Davids answer solves this question. – meteorBuzz Feb 16 '15 at 19:56