skip to Main Content

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:UsersakbarOneDriveРабочий столMusicApp2.0node_modulesmongooselibquery.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:UsersakbarOneDriveРабочий столMusicApp2.0node_modulesmongooselibquery.js:4715:21)
    at model.Query.Query.then (C:UsersakbarOneDriveРабочий столMusicApp2.0node_modulesmongooselibquery.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

here source code

2

Answers


  1. Chosen as BEST ANSWER

    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 });
    });
    

  2. Try a different function

    await Music.findOne({_id: id})
    

    Change

    res.render("./music/edit", { music });
    

    To

    res.render("music/edit", { music });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search