Javascript Express Validator和req.getValidationResult()

Javascript Express Validator和req.getValidationResult(),javascript,node.js,express,Javascript,Node.js,Express,Express Validator模块使用一个新函数:req.getValidationResult()如果验证中出现任何错误,此函数将返回一个对象。该对象看起来像: {param: "field name", msg: "error message", value: "<field input value>"} 函数返回一个数组。但我只需要。味精 req.getValidationResult().then(function(result){ const {msg} =

Express Validator模块使用一个新函数:req.getValidationResult()如果验证中出现任何错误,此函数将返回一个对象。该对象看起来像:

{param: "field name", msg: "error message", value: "<field input value>"}
函数返回一个数组。但我只需要。味精

req.getValidationResult().then(function(result){
    const {msg} = result;
    if(msg != undefined) {
        console.log(msg);
        //return;
    } else {
        console.log('Validation Ok');
}
视图:



代码应如下所示

req.getValidationResult().then(function (result) {
        if (!result.isEmpty()) {
            var errors = result.array().map(function (elem) {
                return elem.msg;
            });
            console.log('There are following validation errors: ' + errors.join('&&'));
            res.render('register', { errors: errors });
        } else {

也许您应该对代码稍加注释。
   request.getValidationResult().then(function(result) {
            if (result.array() != '') {
                response.render('route',{validation:result.array()})
                return;
              return;   
            } else {
               //valid
            }
        });
 <% if(validation) {%>
<ul>
    <% for(var i = 0;i< validation.length;i++){ %>
    <li><%= validation[i].msg %></li>        
    <%}%>
</ul>
<% }%>
req.getValidationResult().then(function (result) {
        if (!result.isEmpty()) {
            var errors = result.array().map(function (elem) {
                return elem.msg;
            });
            console.log('There are following validation errors: ' + errors.join('&&'));
            res.render('register', { errors: errors });
        } else {
req.getValidationResult().then((result) => {
    if(result.isEmpty){
        if(result.array().length > 1){
            return console.log(result.array())
        }else{
            const noticias = {
                titulo: req.body.titulo,
                noticia: req.body.noticia,
                resumo: req.body.resumo,
                autor: req.body.autor,
                data_noticia: req.body.data_noticia
            }
            new noticiasdb(noticias).save().then(() => {
                res.redirect('/noticias')
            }).catch((err) => {
                console.log(`ERRO AO SALVAR ERRO: ${err}`)
            })
        }
    }
})