Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

291
Vistas
CastError: la conversión a ObjectId falló por el valor "ObjectId(610f16a6ba0d5fc2d4593093)"

Estoy tratando de consultar mi modelo de evento usando una ID de referencia para 'artista' para poder mostrar todos los eventos organizados por un artista determinado, pero recibo un error de transmisión. Mi cuarta línea a continuación es cómo intento obtener eventos asociados con un artista determinado:

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

Se requieren ambos modelos en la parte superior de mi código:

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

Así es como se ve uno de mis documentos de eventos mongodb: el artista está en la parte inferior:

 { "_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" }

Aquí está el error completo: ¿qué estoy haciendo mal?

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

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

cuando usa un findById, no necesita volver a emitir un _id encontrado

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda