Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

611
Visualizações
Id used to find object from db is changing on request

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

here source code

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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 });
});
over 4 years ago · Santiago Trujillo Relatório

0

Try a different function

await Music.findOne({_id: id})

Change

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

To

res.render("music/edit", { music });
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda