I am trying to retrieve todo by id. Code for getTodo service method is as below:
const getTodo = async (id, userId) => { const todo = await Todo.findOne({ id: id, userId: userId }); return todo; };
code from router which acts as a controller is as below:
router.get("/:id", tokenValidation.validate, async (req, res) => { const serverResponse = { ...defaultResponse }; try { const decodedToken = jwt.decode( req.headers.authorization.split(" ")[1].trim() ); const getTodo = await todoService.getTodo(req.params.id, decodedToken.id); serverResponse.status = 200; serverResponse.body = getTodo; serverResponse.message = "Get todo by id"; } catch (error) { serverResponse.message = error.message; } return res.status(serverResponse.status).send(serverResponse); });
But the result is always null, on calling:
http://localhost:3000/api/v1/todos/60422718e9db822de8531e87 { "status": 200, "message": "Get todo by id", "body": null }
What is missing in findOne method. Todo with id exists in the collection but is not returned. Please let me know what is missing.
Thanks
https://stackoverflow.com/questions/66501908/mongoose-findone-returns-null March 06, 2021 at 10:53AM
没有评论:
发表评论