Narendra Singh Rathore
Narendra Singh Rathore

Reputation: 706

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.

Upvotes: 0

Views: 379

Answers (1)

Narendra Singh Rathore
Narendra Singh Rathore

Reputation: 706

Credit goes to this post : 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);
   });
});

Upvotes: 1

Related Questions