在辅助函数内调用下一个吗? [英] Calling next inside a helper-function?

查看:41
本文介绍了在辅助函数内调用下一个吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个简单的系统来呈现用户输入错误并停止快速传播,这是我到目前为止所掌握的:

I'm trying to setup a simple system for rendering user-input-errors and stopping propogation in express, this is what I've got so far:

routingFunction= (req, res, next) {
  //setting up a test in express-validator
  var test = req.assert('access_token', 'required').notEmpty();
  isValid(test, next);
  //non error stuff
}

isValid = (tests, next) => {
    //some more code here, that checks if any errors were found and save tem to the array errors.
    if(errors.length > 0){
        return next(new Error());
    }
};

 //a middleware that catches errors:
 app.use((err, req, res, next) => {
     res.json('error').end();
 });

我的问题是,当我调用 Next(new Error()); 时,它不会停止传播,我可以从isValid返回true/false,然后返回next(新的Error()),但这会给我的控制器增加很多膨胀,是否有一些更好的方法可以在helper函数中实现?

My problem with this is that it doesn't stop the propagation when I'm calling Next(new Error());, I could return true/false from isValid and then return next(new Error()), but that would add a lot of bloat to my controllers, is there some better way to do it from within the helper function?

推荐答案

在主路由文件中,例如 routes/index.js

In main route file, e.g. routes/index.js

// Set of func(req, res, next)
let v = require('validator'); // middleware-validators
let user = require('user'); // routes for user 
...
app.get('/smth-route-of-user', v.isTokenSet, v.isEmail, ..., user.get)

middlewares/validator.js

let v = require('validator-func-list');
...
exports.isTokenSet = function (req, res, next) {
    if (v.isValid(req.body.token))
        next(); // Forwarding to next middleware. In our route next() called v.isEmail
    else
        next(new Error('Token is empty')); // Stop route chain and call error-middleware;
}

exports.isEmail = function (req, res, next) {
... 

您可以将验证者加入其中,例如checkUser(),并且仅在路由中使用一个.

You can join validators to one, e.g. checkUser(), and use only one in route.

middlewares/errorHandler.js

module.exports = function (err, req, res, next) {
    let msg = err.message; // Here we see 'Token is empty';

    if (req.xhr)
         res.json(msg);
    else
         res.render('error_page.html', {message: msg, ...});

    // Here we can call next(err) to forwarding error to next errorHandler. In example below it was errorHandler2.
}

app.js 中不要忘记将错误中间件附加到应用程序.

In app.js don't forget to attach error-middleware to application.

app.use(require('middlewares/errorHandler'));
app.use(require('middlewares/errorHandler2'));

如果您需要收集错误,则验证器必须将错误推送到 req.errors (或您想要的其他字段),然后正确地调用 next().在渲染中间件中,您只需检查 req.errors.length 并显示正常或错误页面.

If you need collect errors then validator must push error to req.errors (or another field as you want) and call next() without error. In render middleware you simple check req.errors.length and show normal or error page.

这篇关于在辅助函数内调用下一个吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆