javascriptmeteormeteor-methods

Meteor.call callback is not executed and it's silently ignored if I don't do a fetch


Why if I write this:

/client/Items.js

Template.Items.onCreated(function() {
  console.log('Methor.call');
  Meteor.call('Items.findAll', function (err, resp) {
    console.log('Methor.call callback');
    // Here I will use resp expecting it contains the response
    // returned by the method
    // ...
    return;
  });
  return;
});

/ItemsMethods.js

Meteor.methods({
  'Items.findAll': function () {
    return Items.find({});
  }
});

the callback is silently ignored, i.e. it is not executed and I don't get any error?

Note that if I replace this return Items.find({}); with this return Items.find({}).fetch(); all works as expected.


Solution

  • If you are returning a cursor in a Meteor method, the callback will not be called, because cursors are not serialisable. As the documentation states, Meteor methods should return an EJSON-able value or throw an exception.

    There is actually a feature request on GitHub, which describes this issue in more detail.