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

276
Vistas
Mongoose - Populating a nested array of objects not working

I have a collection called Orders that contains this schema:

const mongoose = require('mongoose');

const orderSchema = new mongoose.Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    restaurant: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Restaurant',
        required: true
    },
    dishes: [
        {
            dish: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Dish'
            },
            amount: Number
        }
    ],
    price: {
        type: Number,
        required: true
    },
    comment: {
        type: String,
        required: false
    },
    status: {
        type: String,
        enum: ['PROCESSING', 'CANCELLED', 'COMPLETED', 'ERROR'],
        default: 'PROCESSING'
    },
    timestamp: {
        type: Date,
        default: Date.now
    }
})

module.exports = mongoose.model('Order', orderSchema);

Inside my router, I have this code:

let orders = await Order.find({restaurant: restaurantID, status:'PROCESSING'}).populate('dishes._id').exec()

Order.find does not throw an exception, but it isnt working either. I want the res.body to look like this:

            {
                "_id": "objectID",
                "user": "objectID",
                "restaurant": "objectID",
                "dishes": [
                    {
                        "amount": number,
                        "dish": {
                            //dish object
                        }
                    },
                    ...
                ],
                //other order properties
            },
            ...
        ]

But for some reason the dishes array looks like this:

        "dishes": [
            {
                "amount": 1,
                "_id": "6184e848e6d1974a0569783d"
            }
        ],

What am I doing wrong?

I know that if populate() worked the res.body dishes array would not have a property called 'dish' and instead have a property called _id that would contain the dish object, but this shouldnt be hard to change once populate() works.

EDIT:

I realised that my createOrder route could be part of the problem since it ignores my schema and uses an id property for the objectID instead of dish. The array I save to the DB contains a property called id for the id instead of dish, but shouldnt my schema throw an exception when i try to save something like this to my database?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

At first glance, I think the problem might be that you have a syntax problem. Try

.populate('dishes').exec()

instead of

.populate('dishes._id').exec()
about 4 years ago · Juan Pablo Isaza Denunciar
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