我正尝试从Mongoose的Mongodb获取一个带有以下代码的商品。在邮递员中发出带有ID的获取请求,以 http://localhost:5000/api/report/l5c959036c3dfe2338412be8e,返回的错误是
“ message”:“投射到ObjectId的值失败 模型“报告”的路径“ _id”处的“ l5c959036c3dfe2338412be8e”
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const cors = require("cors");
const bodyParser = require("body-parser");
mongoose.connect(
"mongodb://localhost:27017/reports",
{ useNewUrlParser: true, useCreateIndex: true }
);
mongoose.connection.on(
"error",
console.error.bind(console, "connection error:")
);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.get("/api/report/:id", (req, res) => {
Report.findById(req.params.id, (err, report) => {
if (err) return res.status(404).send({ message: err.message });
return res.send({ report });
});
});
感谢您的帮助!
答案 0 :(得分:0)
按照注释中的建议,尝试从作为参数传递的objectId中删除“ I”。