简体   繁体   English

如何以良好的格式显示 express-validator 生成的错误消息

[英]How to display a express-validator generated error message in a nice format

Express-Validator error handing is a bit raw and produces not too aesthetical results Express-Validator 错误处理有点原始,并且产生的结果不太美观

    return res.status(422).json({ error: error.array() });

The above will simple do the equal of document.write() and leave nothing but the following message on the screen.以上将简单地执行与 document.write() 相同的操作,并且在屏幕上只留下以下消息。 "{"error":["invalid email address","your password should have min and max length between 8-15","your password should have at least one number","your password should have at least one sepcial character"]} " "{"error":["invalid email 地址","您的密码的最小和最大长度应该在 8-15 之间","您的密码应该至少有一个数字","您的密码应该至少有一个特殊字符" ]} "

The console returns the error object like this:控制台返回错误 object,如下所示:

 {
 error: [
'your password should have min and max length between 8-15',
'your password should have at least one number',
'your password should have at least one sepcial character'
]
}

I have tried我努力了

// const failMsg = JSON.parse("{ error: error.array() }");
if (hasError) {
//req.session.error = `{failMsg}`;

Does not work.不工作。 I would rather embed the error message within我宁愿将错误消息嵌入

  • or a或一个

    element via express templating.元素通过快速模板。 like喜欢

     <% if(err) { %> <p id="errorAlert" style="color: red;"><%= err %></p> <%} %>

    Or simply, how can I that error object as plain text?或者简单地说,我怎么能把错误 object 作为纯文本?

  • After spending some time in the console......I found a solution, maybe some other poor soul will try to find this in the docs to no avail, so might be useful to share the answer, because what is out there on the web or in the docs cannot possibly be deployed to production.在控制台上花了一些时间......我找到了一个解决方案,也许其他一些可怜的灵魂会尝试在文档中找到这个无济于事,所以分享答案可能很有用,因为那里有什么web 或文档中的可能无法部署到生产中。

    const error = validationResult(req).formatWith(({ msg }) => msg);
    // check for errors
    const hasError = !error.isEmpty();
    
    if (hasError) {
    /// if error object is not empty
    var failMsg = { error: error.array() }; // this is what is returned by validator
    var arrOfErrors = failMsg.error;  // the errors will be in this array
    var errors2display = [];           // init array for errors to be displayed to user
    for(let i = 0; i < arrOfErrors.length ; i++){
    errors2display.push(arrOfErrors[i] + "💩") }    // loop through and push whatever is there
    
     var oneStep = errors2display;
     req.session.error = `${oneStep}`;  // this is the express-session middleware but it does not matter.
     return res.redirect("/register");   
    
     }
    

    And in the view并且在视图中

            <% if(err) { %>
            <p id="errorAlert" style="color: red;"><%= err %></p>
            <%} %>
    

    This with some nice CSS modal + a keyframe animation and it look pretty decent, certainly better than the original document.write kinda behavior这与一些不错的 CSS 模态 + 关键帧 animation 看起来相当不错,肯定比原始 document.write 有点行为

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM