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

311
Vistas
Mongodb not returning embedded document as an object when using find() in node.js with express but works fine with findById()

schema:

const mongoose = require ('mongoose');

const workSchema = new mongoose.Schema ({
    role: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Staff_roles'
    },
    movie: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Movie'
    }
})

const staffSchema = new mongoose.Schema ({
    name: {
        type: String,
        required: true,
    },
    works : [workSchema],
    summary: {
        type: String
    }
});

module.exports = mongoose.model ('Staff', staffSchema)

I need to implement this in a different page for movies where I need to show the properties of the fields inside "works".

Works is shown as a normal object with the embedded document displaying correctly when using findById() but it only shows [object] for works when using find().

I need to run the find query as Staff.find({ "works.movies._id" :movie.id }) later on so I need to use find later on and it keeps returning it as [object] which makes it impossible to access the contents of work later on the .ejs file for it.

I already tried populating it with 'works' but that doesn't do anything.

staff router code and result using console logs when using findById():

const staff = await Staff.findById(req.params.id).populate('works.movie works.role').exec()
console.log (staff)
{
  _id: new ObjectId("61850be2d9272be145267f4d"),
  name: 'hideo',
  works: [
    {
      role: [Object],
      movie: [Object],
      _id: new ObjectId("61850be9d9272be145267f55")
    }
  ],
  __v: 0
}

staff router code and result using console logs when using find():

const staff = await Staff.find({id:req.params.id}).populate('works.movie works.role').exec()
console.log (staff)
[
  {
    _id: new ObjectId("61850be2d9272be145267f4d"),
    name: 'hideo',
    works: [ [Object] ],
    __v: 0
  }
]
about 4 years ago · Juan Pablo Isaza
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