Can call a function in a model from a controller with a sail frame?

I have a user model and user controller that I created from the sails command. I just need the call function that I created on the model from the controller, for example:

// user model

module.exports = {
  attributes: {
     name: {
       type: 'string',
       required: true
     }
  },
 CallUserFunction: function(){
   //some code goes here.
 }
}

// userController

module.exports = {
   create: function(req, res){
      User.CallUserFunction();//can i call function in user Model like this? 
  }
}
+4
source share
1 answer

What you are trying to do is not an instance method. Instead of calling only User.CallUserFunction()you need to do something like this:

User model

module.exports = {
  attributes: {
     name: {
       type: 'string',
       required: true
     }
  },
 CallUserFunction: function(){
   //some code goes here.
 }
}

User controller

module.exports = {
   create: function(req, res){
      sails.models.user.CallUserFunction();//can i call function in user Model like this? 
  }
}

edit: forgive any typo, since I'm not in front of my dev workstation, to check this out, I really know her very close to this syntax sails.models.

+2

Source: https://habr.com/ru/post/1524092/


All Articles