populate embed array mongoose 5.0. Nested ref

旧城冷巷雨未停 提交于 2019-12-25 04:00:31

问题


This is the first model. It's located in the folder called models/user.js

'use strict'

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

    var UserSchema = Schema({

        publications: [{
            description: String,
            categories: [{
                type: Schema.Types.ObjectId,
                ref: 'Category.subcategories'
            }]
    }]

The model category. It's located in the folder called models/category.js

'use strict'

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

    var CategorySchema = Schema({
        name: String,
        subcategories: [{
            name: String
        }]
    });

Look that The UserSchema has categories. This one has this reference:

ref: 'Category.subcategories'

I have a folder called controller. The next json is from controller/category.js

'use strict'

var Categories = require('./models/category');


    function getCategories(req, res){

        Categories.find({},(err, categories) =>{
            if(err){
                res.status(500).send({ message: 'Error en la peticion' });
                return;   
            }

            if(!categories){
                res.status(404).send({ message: 'No hay categories' });
                return
            }

            res.status(200).send({ categories });
        });
    }


    module.exports = {
        getCategories
    }

The json of Category looks like this.

{
    "categories": [
        {
            "subcategories": [
                {
                    "_id": "5ae4a8b0a7510e3bd80917db",
                    "name": "subcategory1"
                },
                {
                    "_id": "5ae4a8b0a7510e3bd80917da",
                    "name": "subcategory2"
                },
                {
                    "_id": "5ae4a8b0a7510e3bd80917d9",
                    "name": "subcategory3"
                }
            ],
            "_id": "5ae4a8b0a7510e3bd80917d7",
            "name": "Category1",
            "__v": 0
        },
        {
            "subcategories": [
                {
                    "_id": "5ae4a8b0a7510e3bd80917e0",
                    "name": "subcategory1"
                },
                {
                    "_id": "5ae4a8b0a7510e3bd80917df",
                    "name": "subcategory2"
                }
            ],
            "_id": "5ae4a8b0a7510e3bd80917dc",
            "name": "Category2",
            "__v": 0
        }
    ]
}

Each subcategory2 belongs to one Category.

This one is in controller/user.js

The user whithout populate will looks like this

        User.find({}, (err, publications) => {
                if (err) {
                    res.status(500).send({
                        message: "Error en la peticion " + err
                    });
                    return;
                }

                if (!publications) {
                    res.status(404).send({
                        message: "Publicacion no encontrada"
                    });
                    return;
                }

res.status(200).send(publications);

result

"publications": [
            {
                "description": "abc",
                "categories": [
                    "5ae4a8b0a7510e3bd80917db",
                    "5ae4a8b0a7510e3bd80917da"
                ]

            },
            {
                "description": "abcddvas asa",
                "categories": [
                    "5ae4a8b0a7510e3bd80917e0"
                ]                
            },
]

I need to populate categories when I do this.

User.find().populate('publications.categories').then(function (err, posa) {
            if (err) {
                res.status(500).send(err);
                return;
            }
            res.status(500).send(posa);
            return;
        });

So the final json should look like this:

"publications": [
                {
                    "description": "abc",
                    "categories": [
                        {
                          "_id": "5ae4a8b0a7510e3bd80917db",
                          "name": "subcategory1"
                        },
                        {
                           "_id": "5ae4a8b0a7510e3bd80917da",
                           "name": "subcategory2"
                        }
                    ]

                },
                {
                    "description": "abcddvas asa",
                    "categories": [
                        {
                           "_id": "5ae4a8b0a7510e3bd80917e0",
                           "name": "subcategory1"
                        }
                    ]                
                },
    ]

But The result does not show me the final json.

I see that there is a option called Dynamic References. http://mongoosejs.com/docs/populate.html

The example show this

var userSchema = new Schema({
  name: String,
  connections: [{
    kind: String,
    item: { type: ObjectId, refPath: 'connections.kind' }
  }]
});

Instead of use ref, uses refPath. But for me didn't work that.

I tried to use this

https://github.com/buunguyen/mongoose-deep-populate

but that didn't work for me.

I tried to do a lot of tries. None works for me. I have been searching days and trying a lot of things but I have not been able to resolve this.

来源:https://stackoverflow.com/questions/50136255/populate-embed-array-mongoose-5-0-nested-ref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!