سؤال

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

هل كانت مفيدة؟

المحلول 3

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

نصائح أخرى

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

the sails global object has references to almost everything.

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.

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

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!

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

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);
    ...
  }
};
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top