C.P.
C.P.

Reputation: 1222

How to access/call method defined in models from controller in sails.js?

What is the standard/recommended way to access model inside your controller in Sails.js?

E.g. Suppose I have a method called 'addUser' inside '/model/Users.js'. And now I want to access it from '/controllers/UserController.js'. Then what is the standard way to do it?

Is 'require'ing the model inside controller is best way?

Upvotes: 0

Views: 2074

Answers (1)

piscator
piscator

Reputation: 8699

It's very easy in Sails.js. You don't have to include functions from the model inside the controller manually, as long as you define the functions in the module.exports section of your model.

For example the function addUser in your your model Users.js:

module.exports = {

    addUser: function(username){
        //save user
    }
}

Will be available in your controller UserController.js by calling:

Users.addUser("username");

Upvotes: 2

Related Questions