Reputation: 569
I'm having this little issue/confusion here... I was actually able to get an individual article by their _id using findById(req.params.articleId)
my get request
router.get('/:articleId', function (req, res, next) {
var decoded = jwt.decode(req.query.token);
Article.findById(req.params.articleId, 'keyskeys')
.populate('user')
.exec( function (err, article) {
console.log('see out article too', article)
// if the ID is not found or invalid, return err
if (err) {
return res.status(500).json({
title: 'An error occured',
error: err
});
}
// if the article was not found anyways
if (!article) {
return res.status(500).json({
title: 'Article not found',
error: { message: 'Article was not found!' }
});
}
return res.json({
success: true,
message: 'successful :id',
article: article,
});
});
});
Postman is returning 200 ok
, but the data returned isn't just what I wanted
its just returning the objectID whereas I need it to get the whole object of that particular Id to work with...
I'm kind of confused here, googled around, can't really get my hand on something...
postman is returning
{
"success": true,
"message": "successful :id",
"article": {
"_id": "5b0af26a0321733524c64c91"
}
}
its supposed to return something like
{
"_id" : ObjectId("5b0af26a0321733524c64c91"),
"favoritesCount" : 33,
"title" : "jfdkjkjgfkgfkll",
"description" : "lkfgmfglkgfklgfk",
"body" : "klmfl,,;lvk;lggpog,gf.,gf.gl.",
"username" : "qqqq",
"userId" : ObjectId("5b0af2500321733524c64c90"),
"__v" : 0
}
any help will be greatly appreciated
Upvotes: 1
Views: 1257
Reputation: 6512
findById is a convenience method on the model that's provided by Mongoose to find a document by its _id. The documentation for it can be found here.
Example:
// Search by ObjectId
var id = "56e6dd2eb4494ed008d595bd";
UserModel.findById(id, function (err, user) { ... } );
Functionally, it's the same as calling:
UserModel.findOne({_id: id}, function (err, user) { ... });
for more information refer this Similar Question Link
Upvotes: 1