When I am using req.body.post
in Post.findById
it gives undefined
but when I use direct id
in Post.findById
it workes fine.
Console.log(req.body.post)
give correct id but using it in Post.findById
gives undefined
why undefined
while console.log give correct id
const Post = require('../models/post');
module.exports.create = function(req,res){
console.log(req.body.post);
Post.findById(req.body.post,function(err,post){
console.log(post);
return res.redirect('/');
})
}
Output
62175534d37c9ce165033cee
undefined
when I use direct id which is console.log
print then it works fine.
const Post = require('../models/post');
module.exports.create = function(req,res){
console.log(req.body.post);
Post.findById('62175534d37c9ce165033cee',function(err,post){
console.log(post);
return res.redirect('/');
})
}
Output
{
comment: [],
_id: new ObjectId("62175534d37c9ce165033cee"),
content: 'sdf',
user: new ObjectId("621749248e7de90faa6263d4"),
createdAt: 2022-02-24T09:51:48.621Z,
updatedAt: 2022-02-24T09:51:48.621Z,
__v: 0
}