Populate()猫鼬不返回联接数据

时间:2019-02-12 21:20:16

标签: node.js mongodb mongoose

好吧,我正在尝试从创建文档的用户那里检索姓名和姓氏,但是它不起作用,它仍然返回Mongo的ID

这是我的区域模型

var mongo     = require('mongoose'),
    validator = require('mongoose-unique-validator'),
    Schema    = mongo.Schema


var model = new Schema({
    NAME:       { type: String, required: true, unique: true, max: 50, min: 3 },
    STATUS:     { type: String, default: 'active' },
    ADDED_BY:   { type: Schema.Types.ObjectId, ref: 'users' },
    ADDED_DATE: { type: Date, default: Date.now }


}, {collection :'areas'})

model.plugin( validator, { message: 'The {PATH} is not valid or duplicated' } )
module.exports = mongo.model('Area', model )

这是用户模型

var mongo     = require('mongoose'),
    validator = require('mongoose-unique-validator'),
    Schema = mongo.Schema

var userSchema = new Schema({
    PERSONAL_DATA: {
        NAME:            { type: String, required: [ true, 'The name is necessary' ], max: 50 },
        LAST_NAME:       { type: String, required: [ true, 'the lastname is necessary' ], max: 100 },
        PHOTO:           { type: String, max: 100 },
        BIRTHDAY:        { type: Date },
        MARITIAL_STATUS: { type: Schema.Types.ObjectId, ref: 'maritial_statuses' },
        GENDER:          { type: String, max: 1 },
        EMAIL:           { type: String, required: true  },
    },
    COMPANY_DATA: {
        JOB:      { type: Schema.Types.ObjectId, ref: 'jobs' },
        AREA:     { type: Schema.Types.ObjectId, ref: 'areas' },
        ROLE:     { type: Schema.Types.ObjectId, ref: 'roles' },
        BOSS:     { type: Schema.Types.ObjectId, ref: 'users' },
    }
}, { collection: 'users' } )

model.plugin( validator, { message: 'The {PATH} is not valid or duplicated' } )

module.exports = mongo.model('User', userSchema )

这是我的区域路线

var express = require('express'),
    model   = require('../../models/catalogs/areas'),
    app     = express()


app.get('/:from', (req, res) => {

    var from = parseInt( req.params.from )

    model.find()
        .sort('NAME').populate({ path: 'users', select: 'NAME LAST_NAME'})
        .limit(10).skip(from)
        .exec((error, data) => {
            if (error) {
                return res.status(500).json({
                    success: false,
                    error
                })
            }

            res.status(200).json({
                success: true,
                data
            })
        })
})

module.exports = app

响应是

{
    "success": true,
    "data": [
        {
            "STATUS": "active",
            "_id": "5c547f4adadf433914f72c8c",
            "NAME": "Contabilidad y Finanzas",
            "ADDED_BY": "5c4f562deec6f4defeea759b",
            "ADDED_DATE": "2019-02-01T17:18:02.680Z",
            "__v": 0
        },
        {
            "STATUS": "active",
            "_id": "5c547f3edadf433914f72c8b",
            "NAME": "Tecnologías",
            "ADDED_BY": "5c4f562deec6f4defeea759b",
            "ADDED_DATE": "2019-02-01T17:17:50.579Z",
            "__v": 0
        }
    ]
}

如您所见,ADDED_BY是一个与Users联接的字段,我想检索该信息。我不知道我的代码有什么问题。

0 个答案:

没有答案