Ad

How To Use Find And A Callback In Meteor

- 1 answer

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.

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad