I made a get request but I have an error about _id param it can not be read
app.get(‘/users/:id’, (res, req) => {
const _id = req.params.id
if(_id.length != 24) {
res.status(404).send(`id length must be do not less than 24 digit`)
} else {
User.findById(_id).then((user) => {
if (!user) {
res.status(404).send(`the user id is not found `)
}
res.status(200).send(user)
}).catch((e) => {
res.status(500).send(e)
})
}
})
3
Answers
finally, I get it. it's about id in mongoose must transform it to string and then length must be equal to 12 bytes.
Try:
User.findById({_id:_id})
Tell me if it works