Mongoose populate 3 deep nested schema with JSON response

Viewed 83

I have a find() query that when executed, I can see the json with the nested schemas that I want to see except for the 'artista' attribute only displays the id, instead of the properties I want. See below:

{
    "total": 1,
    "ordenes": [
        {
            "artpieces": [
                {
                    "_id": "60c1388f30316c02b9f6351f",
                    "artista": "60c055736c7ca511055a0e1a",
                    "nombre": "LILIES"
                },
                {
                    "_id": "60c12fca30316c02b9f63519",
                    "nombre": "GERNICA",
                    "artista": "60c136bf30316c02b9f6351b"
                }
            ],
            "_id": "60c414f9ea108a14ef75a9fb",
            "precio": 3000,
            "usuario": {
                "_id": "609c0068e67e68",
                "nombre": "Arturo Filio"
            }
        }
    ]
}

The query I use to get the json above:

const [total, ordenes] = await Promise.all([
    Orden.countDocuments(),
    Orden.find()
      .populate("usuario", "nombre")
      .populate("artpieces", ["nombre","artista","nombre"])
  ]);

  res.json({
    total,
    ordenes
  });

It's an order schema that has artpieces. Each artpiece (I called it 'producto'), has a name a genre, an author/artist and the user which the order belongs to.

my Schema for the orden.js:

const { Schema, model } = require('mongoose');

const OrdenSchema = Schema({
  artpieces: [
    {
      type: Schema.Types.ObjectId,
      required: true,
      ref: 'Producto'
    }
  ],
  estado: {
    type: Boolean,
    default: true,
    required: true
  },
  usuario: {
    type: Schema.Types.ObjectId,
    ref: 'Usuario',
    required: true
  },
  precio: {
    type: Number,
    required: true
  }
})

OrdenSchema.methods.toJSON = function () {
  const { __v, estado, ...data} = this.toObject();
  return data;
}

module.exports = model('Orden', OrdenSchema);

Last thing I want to mention, I know for a fact that I have the code necessary in the artista.js model to display the name of the artist because I have a similar query to display all the artpieces with each artpiece have a genre and an artist.

That example looks like so (to give context):

{
    "total": 4,
    "productos": [
        {
            "precio": 0,
            "_id": "60c12fca30316c02b9f63519",
            "nombre": "GERNICA",
            "categoria": {
                "_id": "60c04e3605d3c10ed10389e4",
                "nombre": "NEO CUBISMO"
            },
            "artista": {
                "_id": "60c136bf30316c02b9f6351b",
                "nombre": "PICASSO"
            },
            "usuario": {
                "_id": "609c8c0068e67e68",
                "nombre": "Arturo Filio"
            }
        }
    ]
}

What am I doing wrong that I can't get my json result at the top look like the json at the bottom, where the artist attribute is?

Also just to point out, I have checked how to nest populate methods in order SO posts including the path and the ref to the Schema and still haven't been able to get the expected result.

0 Answers
Related