0
votes

I'm migrating a project to Sails.js, I decided use Sails because I need to chaing many functions to single path and in it's documentation sais it is posible but I tryed a single example and I can not make it works, when I try to execute two functions on a path i got this error:

Error: next (as in req,res,next) should never be called in an action function (but in action algo/fn1, it was!) It was called with no arguments. Please use a method like res.ok() or res.json() instead.

What am I doing wrong or how can I make it works? this is my code:

routes.js

// ...
'get /chain': [
    'AlgoController.fn1',
    'AlgoController.fn2'
],
// ...

AlgoController.js

let Controller = {};
Controller.fn1 = function(req, res, next) {

    req.executed = ['executed fn1'];
    next();
};

Controller.fn2 = function(req, res, next) {

    req.executed.push('executed fn2');
    res.send(req.executed.join(' and '));
};

module.exports = Controller;

If I delete the next() or use res.ok() / res.json(), the second function is never executed.

1

1 Answers

1
votes

Well, I solved this using req.next() instead next(), so this is the code:

let Controller = {};
Controller.fn1 = function(req, res) {

    req.executed = ['executed fn1'];
    return req.next(); // this is how you call next fn
};

Controller.fn2 = function(req, res) {
    req.executed.push('executed fn2');
    res.send(req.executed.join(' and '));
};

module.exports = Controller;

This works well, hope this help somebody else.