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

301
Visualizações
CastError: Cast to ObjectId failed for value "ObjectId(610f16a6ba0d5fc2d4593093)"

I'm trying to query into my event model using a reference Id for 'artist' so I can display all the events hosted by a given artist but am receiving a cast error. My fourth line below is how I'm trying to pull in events associated with a given artist:

module.exports.showArtist = async (req, res,) => {
    const artist = await Artist.findById(req.params.id).populate('events');
    const artistId = "ObjectId(" + req.params.id + ")";
    const event = await Event.find( { "artist": artistId });
    if (!artist) {
        req.flash('error', 'Cannot find that Artist');
        return res.redirect('/artists');
    }
    res.render('artists/show', { artist });
}

Both models are required at the top of my code:

const Artist = require('../models/artist');
const Event = require('../models/event');

Here's how one of my mongodb event docs looks - artist is towards the bottom:

{ "_id" : ObjectId("6138c78e15832f41e263c601"), 
    "geometry" : 
        { "type" : "Point", 
          "coordinates" : [ -97.748541, 30.269936 ] 
         },
    "event_name" : "Friday night show", 
    "location" : "Austin, Tx 78745", 
    "description" : "Show at our Sam's Town Point", 
    "event_start" : ISODate("2021-09-11T03:00:00Z"), 
    "event_end" : ISODate("2021-09-11T04:25:00Z"), 
    "image" : "https://starbar.com", 
    "created" : ISODate("2021-09-08T14:24:14.399Z"), 
    "artist" : ObjectId("610f16a6ba0d5fc2d4593093"), 
    "__v" : 4, 
    "notification" : "30" 
}

Here's the full error - what am I doing wrong?

CastError: Cast to ObjectId failed for value "ObjectId(610f16a6ba0d5fc2d4593093)" (type string) at path "artist" for model "Event"
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

module.exports.showArtist = async (req, res,) => {
const artist = await Artist.findById(req.params.id).populate('events');
const artistId = artist._id;
const event = await Event.find( { "artist": artistId });
if (!artist) {
    req.flash('error', 'Cannot find that Artist');
    return res.redirect('/artists');
}
res.render('artists/show', { artist });
}

when you use a findById you don't need to recast a found _id

about 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