Getting result from Meteor.call without using Session

I’ve been using Meteor.call as

Meteor.call("myMethod", function (error, result) { // Async call
        if (error) {
            // Error handling code
        } else {
            Session.set("results", result); // Set the result information as a session to retrieve outside of the call
        }
    });

Because I can’t seem to get the result value to outside the call without the session. This is what pretty much every example I’ve seen uses.
However I don’t want to use Session to get the call result value.
My initial theory was that it’s the scope but I declared a variable outside the call and the assigned the result to it and it returns undefined.
Can someone explain (and I do want the explanation of why) if it is really necessary to use Session and if not how to get the result value to outside the call without getting undefined values.

how and where you defined the variable for test ?

Like this:

Template.testStuff.onRendered(function(){
var testVariable;
Meteor.call("myMethod", function (error, result) { // Async call
        if (error) {
            // Error handling code
        } else {
            testVariable = result; // Assign the result information
        }
    });
console.log(testVariable);
});

On the client, if you do not pass a callback and you are not inside a stub, call will return undefined, and you will have no way to get the return value of the method. That is because the client doesn’t have fibers, so there is not actually any way it can block on the remote execution of a method.

From http://docs.meteor.com/#/full/meteor_call

Remember, the method call is asynchronous - if you add console.log("Inside method: ", testVariable) inside the else you’ll see that the console.log outside the method call will return first.

This is why Session is commonly used, since it’s the easiest way to get the return value when it becomes available - making use of Meteor reactivity.

Related package: https://github.com/ianserlin/meteor-sync-methods

1 Like

As previously explained, since the callback is called “later”, when you are doing your console.log, it has not been called yet.
Another solution : https://github.com/stubailo/meteor-reactive-method

1 Like

what has meteor-reactive-method to do with this?

Nothing, it’s just to confuse the original poster… :smirk:

1 Like

That one seems like a dangerous package :slight_smile:

1 Like

Further to @mesosteros answer you probably need to use a ReactiveVar

Template.testStuff.onRendered(function(){
var testVariable = new ReactiveVar();
Meteor.call("myMethod", function (error, result) { // Async call
        if (error) {
            // Error handling code
        } else {
            testVariable.set( result); // Assign the result information
        }
    });
console.log(testVariable.get()); // probably won't be set yet!!!
});

Template.testStuff.helpers({
  testVariable: function() {
    return testVariable.get();
  }
}
1 Like

Ok @TwinTails, but can you please give me an example of a simple stub to use in this case to solve this issue? I don’t really know how a stub looks like in code, as I only know the description of what a stub is.

Check this SO discussion. It may not be your case, but if you need the result value synchronously, check this post.

1 Like