0

I have a Model called User for the users schema and a Model called Project for the projects Schema.

const userSchema = new mongoose.Schema(
  {
    name: { type: String, required: true, unique: true },
    email: {
      type: String,
      unique: true,
      required: true,
    },
    password: { type: String, required: true },
  },
  { timestamps: true }
);




const projectSchema = new mongoose.Schema(
      {
        name: {
          type: String,
        },
        client: {
          type: String,
        },
        developers: [
            {type: mongoose.Schema.ObjectId, ref: 'User'}
        ],
        description: { type: String },
      },
      { timestamps: true }
    );

Developers is going to be an array of ObjectIds so as to get the info from User Schema.

This is how it would look like in the database: enter image description here

What i am trying to do now is to access the name of the user through the developers array. If i do somethink like:

console.log(project.developers.map((developer) => developer));

i only get the id of the user/developer as a string and not the nested info:

enter image description here

But if i console.log developer.name i get undefined.

Is there a way to access the name of the developer through ObjectId?

Francesco
  • 419
  • 1
  • 4
  • 12
  • Solved by using populate function. More info here: https://stackoverflow.com/questions/34985846/mongoose-document-references-with-a-one-to-many-relationship – Francesco Jul 10 '21 at 13:44

0 Answers0