sails.js access controller method from controller

2019-01-14 03:15发布

How come in sails you cannot access other controller methods from within another one?

like this.

module.exports = 

   findStore: ->
       # do somthing

   index: ->
      @findStore(); # Error: undefined

Compiled

module.exports = {
  findStore: function() {},
  index: function() {
    return this.findStore(); // Error: undefined
  }
};

If you can't do this, then why not? how else should I be doing this...

7条回答
霸刀☆藐视天下
2楼-- · 2019-01-14 03:40

It's slightly annoying when you're just trying to build something quickly, but in the long run it forces good code organization practice (by making it harder to shove all business logic into a controller).

查看更多
smile是对你的礼貌
3楼-- · 2019-01-14 03:44

One of the best ways to organize your code in Sails, at least for me and my team, has been to have all the real business logic in Services (/api/services). Those objects can be accessed globally from any controller.

Also, a good practice is working with promises in services (as Sails use them on the model methods)

Just create a Store service (StoreService.js), with your code:

module.exports = {
  findStore: function(storeId) {
    // here you call your models, add object security validation, etc...
    return Store.findOne(storeId);
  }
};

Your Controllers should handle all that is related to requests, calling services, and returning apropriate responses.

For example, in you example, the controller could have this:

module.exports = {
  index: function(req, res) {
    if(req.param('id')) {
      StoreService.findStore(req.param('id'))
        .then(res.ok)
        .catch(res.serverError);
    } else {
      res.badRequest('Missing Store id');
    }
  },
  findStore: function(req, res) {
    if(req.param('id')) {
      StoreService.findStore(req.param('id'))
        .then(res.ok)
        .catch(res.serverError);
    } else {
      res.badRequest('Missing Store id');
    }
  },
};

That way, you have really simple controllers, and all business logic is managed by services.

查看更多
男人必须洒脱
4楼-- · 2019-01-14 03:54

You can do something like this:

//ArticleController
module.exports = {
  findStore: async () => {
    return await findStoreFunc(req.param('id'));
  },
  index: async () => {
    ...
    return await findStoreFunc(id);
  }
};

const findStoreFunc = async (id) => {...}

And to use the function from another controller:

const ArticleController = require('./ArticleController');
//CustomerController
module.exports = {
  index: async () => {
    ...
    let article = await ArticleController.findStore(id);
    ...
  }
};
查看更多
Explosion°爆炸
5楼-- · 2019-01-14 03:55

Having the same problem for last few hours. I used the api/services folder. It may not be exactly what you need but it is an option. A good explanation is here. What services would one add to the api/services folder in sails.js

查看更多
仙女界的扛把子
6楼-- · 2019-01-14 03:55

You can use sails.controllers.yourControllerName.findStore()

the sails global object has references to almost everything.

查看更多
闹够了就滚
7楼-- · 2019-01-14 04:00

A more elegant way to solve this problem is using the keyword this before the function name.

Example:

one: function() {
   console.log('First Function');
},

two: function() {
   // call the function one in the same controller
   this.one();
}
查看更多
登录 后发表回答