在 SailsJS 路径上使用多个函数
Using multiple functions on SailsJS paths
我正在将一个项目迁移到 Sails.js,我决定使用 Sails,因为我需要将许多函数链接到单个路径,并且在它的文档中说这是可行的,但我尝试了一个示例,但我不能让它工作,当我尝试在路径上执行两个函数时出现此错误:
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.
我做错了什么或者我怎样才能让它起作用?
这是我的代码:
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;
如果我删除 next() 或使用 res.ok() / res.json(),则永远不会执行第二个函数。
好吧,我使用 req.next() 而不是 next() 解决了这个问题,所以这是代码:
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;
效果很好,希望对其他人有所帮助。
我正在将一个项目迁移到 Sails.js,我决定使用 Sails,因为我需要将许多函数链接到单个路径,并且在它的文档中说这是可行的,但我尝试了一个示例,但我不能让它工作,当我尝试在路径上执行两个函数时出现此错误:
Error:
next
(as in req,res,next) should never be called in an action function (but in actionalgo/fn1
, it was!) It was called with no arguments. Please use a method likeres.ok()
orres.json()
instead.
我做错了什么或者我怎样才能让它起作用? 这是我的代码:
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;
如果我删除 next() 或使用 res.ok() / res.json(),则永远不会执行第二个函数。
好吧,我使用 req.next() 而不是 next() 解决了这个问题,所以这是代码:
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;
效果很好,希望对其他人有所帮助。