Client getting Undefined for Server method

Here’s my server method that returns the count of documents of a collection.

'doCount': function(){
      var x = ProductList.find().count();
      console.log(x); // logging it correctly , 2
      return x;
  },

and below is my client snippet which gets undefined instead of getting the correct log in the server console.

console.log(Meteor.call('doCount')); // returns undefined

the Collection has been declared in global

ProductList = new Mongo.Collection('players');

This seems strange.
I should rest before answering :smiley:

And ofc mandatory callback as

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.

Old reaction:You need to call method. Like Meteor.call http://docs.meteor.com/#/full/meteor_call

1 Like

thats what i did in the client side.
console.log(Meteor.call('doCount')); // returns undefined

You must use a callback on the client:

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.

So, you want something like:

Meteor.call('doCount', function(error, result) {
  console.log(result);
});
1 Like

You have to do console.log inside of a callback.

Meteor.call('doCount', function(error, result){
  if(error){
    console.error(error)
  }
  else {
    console.log(result)
  }
});

Edit: Looks like robfallows beat me to it, lol.

1 Like

Still i’m like your post too!

By the way, i’m really getting confused by the meaning of callback what is actually meant by it?

Asynchronous part which will get executed after function/method finish.
If you are familiar with Fibers, or Future way in node.js you would know

This is a good introduction. Basically, a callback is a reference to a function which will be executed upon completion of an asynchronous function.

The full answer is here.

Just to pad out the concept of asynchronous code and callbacks a little more:

When you do:

console.log('starting');
Meteor.call('doCount', function(error, result) {
    console.log(result);
}
console.log('finished');

You are likely to see this on the console:

starting
finished
99

In other words, JavaScript does not wait for your call to finish before carrying on. This is a key characteristic of asynchronous code. If you have an asynchronous request (like AJAX or a Meteor.call), which could take several (hundred) milliseconds to complete, you do not want to block the rest of your code while that’s happening.

A callback lets your code carry on until the ayschronous code completes. At that point, the function you registered as the callback (in the above example, it’s an inline, anonymous function) is invoked with a set of parameters specified for that type of callback. For a Meteor.call (and many others) these parameters are error and result. The correct action as @jrudio pointed out in his more complete code sample is to check for an error before using the result.

1 Like