enter image description here building app on express and mongoose mongodb. when trying to get to /musics/:id to get to details page app is sending me to correct page and it is crashing because id is changed
app.get("/musics/:id", async (req, res) => {
console.log(req.params);
const { id } = req.params;
const music = await Music.findById(id);
res.render("./music/edit", { music });
});
here is error
DATABASE CONNECTED!
{ id: '625d0fb066f8544535a2466d' }
{ id: 'Roboto-Regular.ttf' }
C:\Users\akbar\OneDrive\Рабочий стол\MusicApp2.0\node_modules\mongoose\lib\query.js:4715
const castError = new CastError();
^
CastError: Cast to ObjectId failed for value "Roboto-Regular.ttf" (type string) at path "_id" for model "Music"
at model.Query.exec (C:\Users\akbar\OneDrive\Рабочий стол\MusicApp2.0\node_modules\mongoose\lib\query.js:4715:21)
at model.Query.Query.then (C:\Users\akbar\OneDrive\Рабочий стол\MusicApp2.0\node_modules\mongoose\lib\query.js:4814:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
messageFormat: undefined,
stringValue: '"Roboto-Regular.ttf"',
kind: 'ObjectId',
value: 'Roboto-Regular.ttf',
path: '_id',
reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
CodePudding user response:
as suggested in comments changing route url helped
app.get("/musics/:id/details", async (req, res) => {
const { id } = req.params;
const music = await Music.findById(id);
res.render("./music/edit", { music });
});
CodePudding user response:
Try a different function
await Music.findOne({_id: id})
Change
res.render("./music/edit", { music });
To
res.render("music/edit", { music });