ValidatedMethod and Mongo Cursors

I’m not sure why this code does or does not work:

export const EntitySearchSchema = new SimpleSchema({
	searchLimit: {type: Number}
});

export const listEntities = new ValidatedMethod({
	name: 'entities.list',
  validate: EntitySearchSchema.validator(),
  run(searchLimit) {
		if (Meteor.isServer) {
      return {count: Entities.find({}, { limit: searchLimit }).count();};
    }
	}
});

This results in: Exception while invoking method 'entities.list' Error: Match error: Failed Match.OneOf, Match.Maybe or Match.Optional validation from the server, and a fantastically unhelpful client-side error, ending with:

{... isClientSafe: true, error: 400, reason: "Match failed", details: undefined, message: "Match failed [400]", errorType: "Meteor.Error" }
react_devtools_backend.js:2560:23

After many hours of trial-and-error, I found that changing:

return {results: Entities.find({}, { limit: searchLimit }).fetch();};

to

return {results: Entities.findOne('someRandomId'};

returns things just fine.

The call-site is inside a component:

Meteor.call('entities.list',
      {
        searchLimit: 5
      },
      function (error, res){
        if (error) {
          console.error(error);
        } else {
          console.log(res);
        }
      });

I am honestly at a loss as to why the first fails and the second one works. I know that the first is using a cursor… is there a way to force it to resolve and return the data so this [400] Match error stops appearing?

Publications would be nice, but there are too many rows to load client-side, so server-side loading is preferable.

Thanks!

Change the signature of run to:

run ({ searchLimit })
1 Like