2017-10-11 59 views
0

它正在试图查询在其他模型,并得到不确定的实例方法不同型号静态方法,虽然我的架构类以外使用相同的实例方法另一种模式,它的工作。 为了更好的概述,请参阅示例代码:查询在不同模型

UserFormSchema.post('save', function (next) { 
var form = this; 
    Models.SubCategory.getById(form.subCategoryId).then(function(data){ 
    console.log(data); 
    }).catch(function(err){ 
    return Promise.reject(err); 
    }); 
}); 


TypeError: Cannot read property 'SubCategory' of undefined 

每个模式可以定义实例和静态方法的模型。

+0

你可以做一个代码片段 – thedarkone

+0

尝试this.Models.getById – Sam

+0

@Sam尝试,但同样 –

回答

1

幸得这个帖子:https://stackoverflow.com/a/38488773/1936186`

UserFormSchema.post('save', function (next) { 
var form = this; 
mongoose.model('SubCategory').getById(form.subCategoryId).then(function(data){ 
     console.log(data); 
    }).catch(function(err){ 
    return Promise.reject(err); 
    }); 
});