我在mongodb中有一个问题嵌入式数组,mongoose
我有一个模式(cuisineSchema
),在这个模式中,我有一个引用(id' s)到另一个模式(mealCategorySchema
)的数组(嵌入式数组)。我遇到的问题是,当我尝试更新嵌入式阵列时,嵌入式阵列是' null
'而不是' [ ]
' -empty,因此无法调用push方法来推送新项目,我收到错误:
" Cannot Read Property push of null
"
以下代码:
//MONGOOSE MONDELS
var mongoose=require('mongoose');
var cuisineSchema=new mongoose.Schema({
name:String,
mealCategories:[{type:mongoose.Schema.Types.ObjectId,ref:'MealCategory'}]
})
var mealCategorySchema=new mongoose.Schema({
name:String,
description:String,
imageUrl:String
});
// SAVING to mealCategories数组导致错误" cannot read property of null
"
Repo.Repository.prototype.addCategory=function(id,callback){
this.Model.findById(id,function(err,doc){
if(err){
var error={error:500,message:err}
callback(error);
}
//The error is thrown here at the push method
//ERROR: CANNOT READ property push of null
doc.mealCategories.push(id)
}
为什么数组null
而不是[ ]
,我的mongoose模型声明有问题?