sails.js access controller method from controller method

爱⌒轻易说出口 提交于 2019-12-28 03:54:08

问题


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...


回答1:


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




回答2:


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

the sails global object has references to almost everything.




回答3:


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:


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).




回答5:


I would like to suggest a solution that works but not the best possible way to do it. We can use bind function to bind the context with the calling source as shown below :

generateUrl is present in the Controller A

function generateUrl(){
  return 'www.google.com';
}

get URL is another method in Controller A

getURL(){
  A.generateURL.bind(A.generateURL) //func call with optional arg
}

I hope this helps!




回答6:


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();
}



回答7:


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);
    ...
  }
};


来源:https://stackoverflow.com/questions/20735380/sails-js-access-controller-method-from-controller-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!