所以,我正在使用快递。我想使用next()将控制器链接在一起。
我有三个。他们做不同的事情,我想把它们混合在一起。 checkEmailVerification如果不好则转到失败页面,如果好则转到next()。另外两个就是下一个()。 completeE()会将它全部包装起来。
app.core.users.checkEmailVerification,
app.core.users.processVerification,
app.mean.subscribeMarketingEmails,
app.core.users.completeEmailVerification
我的第一个想法是将其表达为:
// Confirm users email
app.route('/auth/confirm/:confirmationCode').get(
function(req, res, next) {
app.core.stack(req, res, next, [
app.core.users.checkEmailVerification,
app.core.users.processVerification,
app.mean.subscribeMarketingEmails
]);
}
);
所以,我将下面的函数编写为我自己的编程挑战。
它不起作用。 C'est la vi。所以我的两个问题是:
最好的方法是什么?可能已经为此做了一些事情。 编辑:也许就是这样。 //确认用户电子邮件 。app.route( '/认证/确认/:confirmationCode')得到(app.core.users.checkEmailVerification); 。app.route( '/认证/确认/:confirmationCode')得到(app.core.users.processVerification); 。app.route( '/ AUTH /确认/:confirmationCode')得到(app.mean.subscribeMarketingEmails);
你能看到我在代码中遗漏了什么吗?递归不是更新'当前'。输出如下:
谢谢!
// FIXME: this needs work
/**
* Stack req controllers through the use of next()
*
* E.g. app.core.stack(req, res, [
first,
second,
third
])
*
* == more or less ==>
*
* first(req, res, second(req, res, third(req, res)))
*/
core.stack = function(req, res, next, controllers, current) {
// current defaults to 0;
current = current || 0;
console.log('--> Current in stack:'+current);
// Check if something went wrong.
if (current < 0 || current >= controllers.length)
throw "core.stack() parameter current is invalid";
// If this is the last controller. Complete.
if (current == controllers.length-1)
return controllers[current](req, res, next);
// We're in the middle somewhere. Wrap in core.stack().
current++;
function wrap() {
console.log('--> Current in wrapper:'+current);
return core.stack(req, res, controllers, current)
}
return controllers[current](req, res, wrap);
}
输出:
--> Current in stack:0
TypeError: Property '1' of object function (req, res, next) {
if (true) {
console.log("checking. Success!");
// process the next action in the route
next();
} else {
// on failure, flash a message.
console.log('Huh. That didn\'t work. Login and click the resend verification email link');
}
},, is not a function
at Object.core.stack (/home/michael/scm/mean-core/core.js:47:32)
at Object.handle (/home/michael/scm/writermustwrite.com/app/routes/users.server.routes.js:24:13)
at next_layer (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/route.js:107:5)
at c (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:195:24)
at param (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:270:14)
at param (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:282:16)
at Function.proto.process_params (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:298:3)
at next (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:189:19)
at next (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:166:38)
GET /auth/confirm/adsfasdfasdf 500 61ms
--> Current in stack:0
TypeError: Property '1' of object function (req, res, next) {
if (true) {
console.log("checking. Success!");
// process the next action in the route
next();
} else {
// on failure, flash a message.
console.log('Huh. That didn\'t work. Login and click the resend verification email link');
}
},, is not a function
at Object.core.stack (/home/michael/scm/mean-core/core.js:47:32)
at Object.handle (/home/michael/scm/writermustwrite.com/app/routes/users.server.routes.js:24:13)
at next_layer (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/route.js:107:5)
at c (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:195:24)
at param (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:270:14)
at param (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:282:16)
at Function.proto.process_params (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:298:3)
at next (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:189:19)
at next (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:166:38)
--> Current in stack:0
TypeError: Property '1' of object function (req, res, next) {
if (true) {
console.log("checking. Success!");
// process the next action in the route
next();
} else {
// on failure, flash a message.
console.log('Huh. That didn\'t work. Login and click the resend verification email link');
}
},, is not a function
at Object.core.stack (/home/michael/scm/mean-core/core.js:47:32)
at Object.handle (/home/michael/scm/writermustwrite.com/app/routes/users.server.routes.js:24:13)
at next_layer (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/route.js:107:5)
at c (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:195:24)
at param (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:270:14)
at param (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:282:16)
at Function.proto.process_params (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:298:3)
at next (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:189:19)
at next (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:166:38)
答案 0 :(得分:2)
对于错误,您可以调试(https://github.com/node-inspector/node-inspector)并查看控制器功能内部发生的情况。
对于逻辑,我认为你缺少第一个控制器调用,因为当前++在控制器[current]之前,所以你永远不会调用第一个控制器。
对于解决方案,您可以为同一路径添加多个路由处理程序。 next
变量指向同一路由的下一个处理程序。所以,你会用简化的代码实现同样的目标。
app.route('/auth/confirm/:confirmationCode')
.get(app.core.users.checkEmailVerification)
.get(app.core.users.processVerification)
.get(app.mean.subscribeMarketingEmails);
甚至是
app.route('/auth/confirm/:confirmationCode').get(
app.core.users.checkEmailVerification,
app.core.users.processVerification,
app.mean.subscribeMarketingEmails);
控制器功能将通过req,res和next对象。