2

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.

Andrea
  • 15,900
  • 18
  • 65
  • 84

2 Answers2

2

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.

Matthias A. Eckhart
  • 5,136
  • 4
  • 27
  • 34
  • 1
    Thanks. Now I understand. I hope in the future will be thrown an exception (at least server side) for these cases. – Andrea Dec 15 '15 at 20:47
1

Items.find({}); return a cursor that is a sort of pointer to the data retrieved.

If you use Items.find({}).fetch(); you are returning an array of objects.

perusopersonale
  • 896
  • 1
  • 8
  • 18
  • Thanks for clarify this. I still do not understand why in the first case the `Meteor.call`'s callback is not executed? Is it related with the type of data the method return? – Andrea Dec 15 '15 at 20:01