我正在使用 findOne() 方法使用以下代碼搜索我的資料庫
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");
}
});
});
在郵遞員中使用以下 URL
http://localhost:3000/articles/626e950e4fb74295f5139b78
并為資料庫中的以下記錄傳遞物件 ID
{
"_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
}
但是我的 find one code 回傳no article found,即使該文章存在
收集模式
const articleSchema = {
title: String,
content: String
};
const Article = mongoose.model("Article", articleSchema);
uj5u.com熱心網友回復:
在嘗試了不同的解決方案后,我終于通過在模式中添加 _id: 字串解決了問題
const articleSchema = {
_id: String,
title: String,
content: String
};
其余代碼將保持不變
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/470264.html
下一篇:帶有條件的mongodb更新