1

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
  }
]
kevin
  • 11
  • 2

0 Answers0