在其他模型的实例方法中查询另一个模型 [英] Query another model in instance method of a different model
本文介绍了在其他模型的实例方法中查询另一个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
它试图在另一个模型的实例方法中查询另一个模型静态方法,并且变得不确定,尽管我在架构类之外使用了相同的方法并且可以正常工作. 要获得更好的概述,请参见示例代码:
It am trying to query a different Model static method in instance method of another model, and getting undefined, though i am using same outside the schema class and it's working. For better overview, see sample code:
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
每个模式都可以为其模型定义实例和静态方法.
Each Schema can define instance and static methods for its model.
推荐答案
信用转到这篇文章: 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);
});
});
这篇关于在其他模型的实例方法中查询另一个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文