Bren
Bren

Reputation: 3696

How to use find and a callback in Meteor

I have this function in Meteor:

Posts.find({_id: {$ne: ls._id}}, function(err, item) {
    if (err) {return console.error(err)};
    console.log("item: " + String(item));
});

But I am getting this error:

Exception while invoking method 'updateSuggestions' Error: Match error: Failed Match.OneOf or Match.Optional validation

This does work though:

Posts({_id: {$ne: ls._id}});

So I think its something with the callback function but I'm really not sure what.

Upvotes: 1

Views: 2535

Answers (1)

David Weldon
David Weldon

Reputation: 64332

find doesn't take a callback as a parameter.

On the client, find is synchronous so the callback is unnecessary. On the server, find appears synchronous due to meteor's use of fibers.

Either way, you want something like this:

let posts = Posts.find({_id: {$ne: ls._id}}).fetch();
console.log(posts);

See common mistakes for more details on find and fetch.

Upvotes: 4

Related Questions