mstorkson mstorkson -4 years ago 124
Javascript Question

Meteor callbacks returning undefined

So I've recently started using Meteor.js, and although I appreciate its power, I'm having some issues with the way some of it works. I have a very basic couple of functions that I'm trying to use to get the hang of returning data to the client.

In my server folder:

Meteor.methods({
"thing": (number) => {
return number;
}
});


And then when it is used:

const five = Meteor.call("thing", 5);
console.log(five);


But all I get is
undefined


This is so basic, I assume I must have a fundamental misunderstanding about how this process is supposed to work. I have tried reading up on similar questions, but they mostly reference asynchronous processes, whereas this shouldn't wait on anything.

Answer Source

any communication from client to server is asynchronous, including Meteor methods. you're using a synch return value, which isn't what you're expecting.

make it more like this:

    Meteor.call('thing', function(error, result) {
        if (error) {
            alert(error);
            return;
        }

        console.log(result);
    });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download