WI HERMES
WI HERMES

Reputation: 11

Make many populate in deep populate, 2nd level

I am doing a query of nested objects using populate and when wanting to access a population of the second level, it does not recognize me if not the last population

I'm working under nodejs and mongodb, using the mongoose library

For the population of 'tours_favoritos' only recognizes me the last: 'guia' with its population of 'idiomas'. I do not populate 'sitios', nor 'turistas', nor 'categoria', nor 'empresa_turismo'. If I change the order of the population, only the last one populates me.

servicev1.addGustoFavorito = function (id_turista, id_gusto) {
return new Promise(function (resolve, reject) {
    GustoTurista.findById(id_gusto, function (error, gusto) {
        if (!error) {
            var query = Turista.findByIdAndUpdate(id_turista, { $push: { gustos: gusto._id } }, { new: true });
            query.
                populate({
                    path: 'empresas_favoritas', model: EmpresaTurismo
                }).
                populate({
                    path: 'tours_favoritos',
                    model: Tour,
                    populate: { path: 'sitios', model: Sitio },
                    populate: { path: 'turistas', model: Turista },
                    populate: { path: 'categoria', model: CategoriaTour },
                    populate: { path: 'empresa_turismo', model: EmpresaTurismo },
                    populate: {
                        path: 'guia', model: Guia,
                        populate: { path: 'idiomas', model: Idioma }
                    }
                }).
                populate({
                    path: 'gustos', model: GustoTurista
                })
                .exec(function (err, success) {
                    if (err) {
                        return reject(err);
                    }
                    else {
                        return resolve(success);
                    }
                })
        } else {
            return reject(error);
        }
    })
})

}

See code

Upvotes: 0

Views: 131

Answers (1)

Ravi Shankar Bharti
Ravi Shankar Bharti

Reputation: 9268

The mistake here is in this second populate query:

.populate({
    path: 'tours_favoritos',
    model: Tour,
    populate: { path: 'sitios', model: Sitio },
    populate: { path: 'turistas', model: Turista },
    populate: { path: 'categoria', model: CategoriaTour },
    populate: { path: 'empresa_turismo', model: EmpresaTurismo },
    populate: {
        path: 'guia', model: Guia,
        populate: { path: 'idiomas', model: Idioma }
    }
})

You are passing an object to populate function, and everytime you are using another populate inside that object, it overrides the old one, it is similar to any object definition.

Eg:

var a={
  foo : "bar",
  foo : "bar2",
  foo : "bar3"
}

//the last foo overrides the first two
//a.foo = bar3

just like above example, last populate is overriding the previous populates.

Now back to solve your question,

You need to pass array to populate field, so that it populates all the given fields you want

Try this:

.populate({
    path: 'tours_favoritos',
    model: Tour,
    populate: [
        { path: 'sitios', model: Sitio },
        { path: 'turistas', model: Turista },
        { path: 'categoria', model: CategoriaTour },
        { path: 'empresa_turismo', model: EmpresaTurismo },
        {
            path: 'guia', model: Guia,
            populate: { path: 'idiomas', model: Idioma }
        }
    ]
})

replace the above code in place of your second populate query and it will work.

Upvotes: 0

Related Questions