【发布时间】:2021-06-02 23:34:06
【问题描述】:
const postSchema = mongoose.Schema({
id: String,
name: String,
isbn: String,
image: String,
})
var PostMessage = mongoose.model('PostMessage', postSchema);
const getBook = async (req, res) => {
const { id } = req.params;
try {
const post = await PostMessage.findById(id);
res.status(200).json(post);
} catch (error) {
res.status(404).json({ message: error.message });
}
}
我想通过 "id" 从我的 mongodb 获取数据。如果我的 id 与 mongodb 中的 id 值匹配,它会获取该对象,但它会抛出错误:
{“message”:“模型“PostMessage”的路径“_id”中的值“s-CoAhDKd”转换为 ObjectId 失败”}
【问题讨论】:
标签: node.js reactjs mongodb mongoose mongoose-schema