1

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

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
yemiOdetola
  • 569
  • 4
  • 13
  • 1
    The second parameter to `findOne` is the names of the fields to include and you've only listed `'keyskeys'`. What are you trying to do with that? – JohnnyHK May 31 '18 at 00:39
  • `keyskeys` is just a token I'll need to pass to allow me to access that particular route... – yemiOdetola May 31 '18 at 00:46
  • 1
    Not sure why you think you need to pass `keyskeys` in there, but the second parameter for `findOne` tells Mongoose what fields you want returned, as JohnnyHK said. Since you don't have any `keyskeys` field in your document, only the `_id` is returned. If you just remove the `keyskeys` parameter completely, you should get your desired output. – Sven May 31 '18 at 01:05
  • thanks for the heads up @Sven ... I can see my mistake now... – yemiOdetola May 31 '18 at 01:09

1 Answers1

1

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

Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54