Reputation: 1791
Here is my code on the server:
Meteor.publish('currentRequest', function (requestId) {
console.log('from publish');
console.log(requestId) // The id is printed successfully
console.log(Requests.findOne({_id: requestId})) // returns undefined
return Requests.findOne({_id: requestId});
});
The item ID is printed but .findOne()
doesn't seem to work as it returns undefined
.
What am I doing wrong here?
Upvotes: 1
Views: 4034
Reputation: 10076
The answer to your question will be: because there is no document satisfying your search query.
According to documentation:
Finds the first document that matches the selector, as ordered by sort and skip options. Returns
undefined
if no matching document is found.Equivalent to
find(selector, options).fetch()[0]
withoptions.limit = 1
.
Also, as it has been pointed by @GaëtanRouziès, this publication won't work, because .findOne
returns document/undefined
instead of cursor.
Upvotes: 2
Reputation: 155
.findOne() return the response in asynchronus way. you need to pass a callback function to findOne and use return statement in callback function. Please take a look at the sample code below.
CollectionName.findOne({
query : query
}, function(err, resp) {
if (err) {
throw err;
} else {
return resp;
}
});
Upvotes: -1