Javascript 猫鼬使用困难';填充';

Javascript 猫鼬使用困难';填充';,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,早上好 因此,我似乎在用Node.js和Mongoose填充字段时遇到了问题。它只是打开一个空白数组: 结果:(JSON) 下面是我正在使用的调用populate函数的代码,以及我正在为每个数据使用的模型。这很奇怪 *school.js(模型)* // Require modules within our file: const mongoose = require('mongoose') const schoolSchema = new mongoose.Schema({ name:

早上好

因此,我似乎在用Node.js和Mongoose填充字段时遇到了问题。它只是打开一个空白数组:

结果:(JSON)

下面是我正在使用的调用populate函数的代码,以及我正在为每个数据使用的模型。这很奇怪

*school.js(模型)*

// Require modules within our file:
const mongoose = require('mongoose')

const schoolSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        unique: true,
        trim: true
    },
    website: {
        type: String,
        required: true,
        trim: true
    },
    logo: {
        type: Buffer
    },
    courseassignments: [{
        type: mongoose.Schema.Types.ObjectID, 
        ref: 'CourseAssignment'
    }]
})

// Export the user to use within other files:
const school = mongoose.model('School', schoolSchema)
module.exports = school
router.get('/school', async (req, res) => {

    const schools = await School.find({}).populate({ path: 'courseassignments' })
    res.send(schools)

})
courseasignment.js(模型)

*获取数据的代码:(在app.js中)*

// Require modules within our file:
const mongoose = require('mongoose')

const schoolSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        unique: true,
        trim: true
    },
    website: {
        type: String,
        required: true,
        trim: true
    },
    logo: {
        type: Buffer
    },
    courseassignments: [{
        type: mongoose.Schema.Types.ObjectID, 
        ref: 'CourseAssignment'
    }]
})

// Export the user to use within other files:
const school = mongoose.model('School', schoolSchema)
module.exports = school
router.get('/school', async (req, res) => {

    const schools = await School.find({}).populate({ path: 'courseassignments' })
    res.send(schools)

})

我将从学校模型中删除courseassigment ref,并利用虚拟填充

以下是步骤:

  • school.js(学校模型-如您所见,我删除了courseassignments ref,并添加了虚拟功能选项)
在这一点上,当您到达学校端点时,您的响应将如下所示。 (我只显示一个短项。)

如果你也想访问课程名称(我认为这很好)

  • courseasignment.js
这样,结果将包含与课程相关的字段,如课程名称

[[
    {
        "_id": "5db5a809cfc9951770d5078a",
        "name": "school 1",
        "website": "school 1 website",
        "__v": 0,
        "courseassignments": [
            {
                "_id": "5db5a892cfc9951770d50790",
                "school": "5db5a809cfc9951770d5078a",
                "course": {
                    "_id": "5db5a847cfc9951770d5078d",
                    "name": "course 1",
                    "__v": 0
                },
                "__v": 0,
                "id": "5db5a892cfc9951770d50790"
            },
            {
                "_id": "5db5a89ccfc9951770d50791",
                "school": "5db5a809cfc9951770d5078a",
                "course": {
                    "_id": "5db5a851cfc9951770d5078e",
                    "name": "course 2",
                    "__v": 0
                },
                "__v": 0,
                "id": "5db5a89ccfc9951770d50791"
            },
            {
                "_id": "5db5a8a1cfc9951770d50792",
                "school": "5db5a809cfc9951770d5078a",
                "course": {
                    "_id": "5db5a858cfc9951770d5078f",
                    "name": "course 3",
                    "__v": 0
                },
                "__v": 0,
                "id": "5db5a8a1cfc9951770d50792"
            }
        ],
        "id": "5db5a809cfc9951770d5078a"
    },
    ...
    ...
]
文件:

[
    {
        "_id": "5db5a809cfc9951770d5078a",
        "name": "school 1",
        "website": "school 1 website",
        "__v": 0,
        "courseassignments": [
            {
                "_id": "5db5a892cfc9951770d50790",
                "school": "5db5a809cfc9951770d5078a",
                "course": "5db5a847cfc9951770d5078d",
                "__v": 0
            },
            {
                "_id": "5db5a89ccfc9951770d50791",
                "school": "5db5a809cfc9951770d5078a",
                "course": "5db5a851cfc9951770d5078e",
                "__v": 0
            },
            {
                "_id": "5db5a8a1cfc9951770d50792",
                "school": "5db5a809cfc9951770d5078a",
                "course": "5db5a858cfc9951770d5078f",
                "__v": 0
            }
        ],
        "id": "5db5a809cfc9951770d5078a"
    },
    ...
    ...
]

const mongoose = require('mongoose')

const courseAssignmentSchema = new mongoose.Schema({
    school: {
        type: mongoose.Schema.Types.ObjectID,
        required: true,
        ref: 'School'
    },
    course: {
        type: mongoose.Schema.Types.ObjectID,
        required: true,
        ref: 'Course'
    }
}, {
    toJSON: { virtuals: true },
    toObject: { virtuals: true }
})

courseAssignmentSchema.pre(/^find/, function (next) {

    this.populate({
        path: 'course'
    });
    next();
});


// an index may be required like this
//courseAssignmentSchema.index({ school: 1, course: 1 }, { unique: true });

const courseAssignment = mongoose.model('CourseAssignment', courseAssignmentSchema)
module.exports = courseAssignment

[[
    {
        "_id": "5db5a809cfc9951770d5078a",
        "name": "school 1",
        "website": "school 1 website",
        "__v": 0,
        "courseassignments": [
            {
                "_id": "5db5a892cfc9951770d50790",
                "school": "5db5a809cfc9951770d5078a",
                "course": {
                    "_id": "5db5a847cfc9951770d5078d",
                    "name": "course 1",
                    "__v": 0
                },
                "__v": 0,
                "id": "5db5a892cfc9951770d50790"
            },
            {
                "_id": "5db5a89ccfc9951770d50791",
                "school": "5db5a809cfc9951770d5078a",
                "course": {
                    "_id": "5db5a851cfc9951770d5078e",
                    "name": "course 2",
                    "__v": 0
                },
                "__v": 0,
                "id": "5db5a89ccfc9951770d50791"
            },
            {
                "_id": "5db5a8a1cfc9951770d50792",
                "school": "5db5a809cfc9951770d5078a",
                "course": {
                    "_id": "5db5a858cfc9951770d5078f",
                    "name": "course 3",
                    "__v": 0
                },
                "__v": 0,
                "id": "5db5a8a1cfc9951770d50792"
            }
        ],
        "id": "5db5a809cfc9951770d5078a"
    },
    ...
    ...
]