猫鼬子文档field.id给出未定义

时间:2018-12-18 23:11:45

标签: node.js mongodb mongoose mongoose-schema subdocument

在此代码中,我需要使用其通用ID来取得batch_results,如 猫鼬文件

WHERE bitand(bits,(select sum(distinct organ_id) from organ where organ_id in (&organs))) !=0

使用此代码,我尝试获取输出,

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const config = require('../config/config');



// Result schema
const ResultsSchema = new Schema({
    subject_id: {
        type: mongoose.SchemaTypes.ObjectId,
        required: false
    },
    marks: {
        type: [String], // when needed to store absent as well
        required: false
    }
});

// Batch Schema
const BatchSchema = new Schema({
    student_base_no: {
        type: mongoose.SchemaTypes.ObjectId,
        required: false,
    },
    results: [ResultsSchema]
});

// Marks schema
const MarksSchema = new Schema({

    date: {
        type: Date,
        default: Date.now,
        required: true
    },
    year: {
        type: String,
        default: (new Date).getFullYear(),
        required: true
    },
    batch: {
        type: String,
        required: true
    },
    batch_results: [BatchSchema]

});



const Marks = module.exports = mongoose.model('marks', MarksSchema, 'marks');

,但一直说无法读取未定义的属性'id'

我尝试了很多,但是效果不好:( 请帮助我...

1 个答案:

答案 0 :(得分:1)

您需要更改批处理并标记架构:

const BatchSchema = new Schema({
    student_base_no: {
        type: mongoose.SchemaTypes.ObjectId,
        required: false,
    },
    results:{
         type: Schema.Types.ObjectId,
          ref: 'results' 
       }
})