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

277
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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