I'm using findOne() method to search through my db using the following code
app.get("/articles/:articleId", (req, res) => {
Article.findOne({ _id: req.params.articleId}, (err, foundArticle) => {
if(foundArticle) {
res.send(foundArticle);
} else {
res.send("No article found");
}
});
});
using the following URL in postman
http://localhost:3000/articles/626e950e4fb74295f5139b78
and passing the object id for the following record in the DB
{
"_id": "626e950e4fb74295f5139b78",
"title": "Albert Einstein",
"content": "Did you know that sleep is good for your brain? Einstein sure did, he slept for 10 hours a day!",
"__v": 0
}
but My find one code returns no article found, even though the article is present
collection modal
const articleSchema = {
title: String,
content: String
};
const Article = mongoose.model("Article", articleSchema);
CodePudding user response:
after trying different solution I finally got to solve the issue by adding a _id: string in the schema solves the issue
const articleSchema = {
_id: String,
title: String,
content: String
};
rest of the code will remain the same