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

304
Visualizações
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 à 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