在猫鼬模式中嵌套对象数组

时间:2019-01-08 07:08:59

标签: node.js json mongoose-schema

我正在创建一个Student数据模式,其中一个学生包含每个学期的分数。我想创建一个Sem数组,其中Sem_Schema必须是ref对象。通过使用Sem_Schema,我想创建Sem_Schema对象的sem数组。请帮忙。

var Student_Data_Schema = new mongoose.Schema({
name:{type:String,default:"Rahul Kandiboina", required: true},
reg_no:{type:String,default:"315175711057", required: true},
dob:{type:String,default:"july 3 1998"},
BG:{type:String,default:"o +ve", required: true},
ctg:{type:String,default:"B", required: true},
caste:{type:String,default:"BC", required: true},
EAMCET_Rank:{type:String,default:"29000", required: true},
ECET_Rank:{type:String,default:"123", required: true},
SSC_M:{type:String,default:"9.3", required: true},
Inter_M:{type:String,default:"960", required: true},
DEP_M:{type:String,default:"870", required: true},
BTECH_AGG:{type:String,default:"7.5", required: true},
GRE:{type:String,default:"60", required: true},
GATE:{type:String,default:"120", required: true},
CAT:{type:String,default:"10000", required: true},
TOEFL:{type:String,default:"100", required: true},
E_MAIL:{type:String,default:"rahulkandiboina9@gmail.com", required: 
true},
// Father :{},
// Mother :{},
// Bro :{},
// Sis :{},
Pre_Add :{
            D_no:{type:String},
            Street:{type:String},
            village:{type:String},
            town:{type:String},
            District:{type:String},
            State:{type:String},
            Pin:{type:String}
            },
Per_Add:{
    D_no:{type:String},
    Street:{type:String},
    village:{type:String},
    town:{type:String},
    District:{type:String},
    State:{type:String},
    Pin:{type:String}
},
Marks:{
    CGPA:{type:Number},
    B_LOGS:{type:Number},
    Sem :[Sem_Schema]
}
})



var Sem_Schema =  new mongoose.Schema({
SGPA :{type:String},
CGPA :{type:String},
B_Logs :{type:String},
Sem_Att :{type:String},
T_Sess :{type:String}
})

我想将Sem_Schema嵌套到Sem数组中,但是错误像这样

  

F:\ Project \ WEB \ NewPro \ NodeJs \ node_modules \ mongoose \ lib \ schema.js:398
  抛出新的TypeError('模式数组路径' + prefix + key + '的无效值'); ^

     

TypeError:模式数组路径Marks.Sem的值无效   模式添加   (F:\ Project \ WEB \ NewPro \ NodeJs \ node_modules \ mongoose \ lib \ schema.js:398:13)   在Schema.add   (F:\ Project \ WEB \ NewPro \ NodeJs \ node_modules \ mongoose \ lib \ schema.js:407:14)   在新的架构   (F:\ Project \ WEB \ NewPro \ NodeJs \ node_modules \ mongoose \ lib \ schema.js:114:10)   在对象。   (F:\ Project \ WEB \ NewPro \ NodeJs \ Models \ dept.js:3:27)在Module._compile   (internal / modules / cjs / loader.js:688:30)在   Object.Module._extensions..js(内部/模块/cjs/loader.js:699:10)   在Module.load(internal / modules / cjs / loader.js:598:32)在   tryModuleLoad(内部/模块/cjs/loader.js:537:12)在   Function.Module._load(internal / modules / cjs / loader.js:529:3)位于   Module.require(internal / modules / cjs / loader.js:636:17)在require   (内部/模块/cjs/helpers.js:20:18)。   (F:\ Project \ WEB \ NewPro \ NodeJs \ Router \ college.js:3:14)在   Module._compile(内部/模块/cjs/loader.js:688:30)位于   Object.Module._extensions..js(内部/模块/cjs/loader.js:699:10)   在Module.load(internal / modules / cjs / loader.js:598:32)在   tryModuleLoad(内部/模块/cjs/loader.js:537:12)

1 个答案:

答案 0 :(得分:0)

像这样在Student_Data_Schema之前声明您的Sem_Schema。这里发生的是,当您在架构后引用此对象时,它不知道这是什么导致Sem_schema。因此将代码更改为

var Sem_Schema =  new mongoose.Schema({
SGPA :{type:String},
CGPA :{type:String},
B_Logs :{type:String},
Sem_Att :{type:String},
T_Sess :{type:String}
})

// after this add the next schema

var Student_Data_Schema = new mongoose.Schema({
name:{type:String,default:"Rahul Kandiboina", required: true},
reg_no:{type:String,default:"315175711057", required: true},
dob:{type:String,default:"july 3 1998"},
BG:{type:String,default:"o +ve", required: true},
ctg:{type:String,default:"B", required: true},
caste:{type:String,default:"BC", required: true},
EAMCET_Rank:{type:String,default:"29000", required: true},
ECET_Rank:{type:String,default:"123", required: true},
SSC_M:{type:String,default:"9.3", required: true},
Inter_M:{type:String,default:"960", required: true},
DEP_M:{type:String,default:"870", required: true},
BTECH_AGG:{type:String,default:"7.5", required: true},
GRE:{type:String,default:"60", required: true},
GATE:{type:String,default:"120", required: true},
CAT:{type:String,default:"10000", required: true},
TOEFL:{type:String,default:"100", required: true},
E_MAIL:{type:String,default:"rahulkandiboina9@gmail.com", required: 
true},
// Father :{},
// Mother :{},
// Bro :{},
// Sis :{},
Pre_Add :{
            D_no:{type:String},
            Street:{type:String},
            village:{type:String},
            town:{type:String},
            District:{type:String},
            State:{type:String},
            Pin:{type:String}
            },
Per_Add:{
    D_no:{type:String},
    Street:{type:String},
    village:{type:String},
    town:{type:String},
    District:{type:String},
    State:{type:String},
    Pin:{type:String}
},
Marks:{
    CGPA:{type:Number},
    B_LOGS:{type:Number},
    Sem :[Sem_Schema]
}
})