Express validator and req.getValidationResult ()

The Express-Validator module uses a new function: req.getValidationResult () . This function returns an object if there are validation errors. The object looks like this:

{param: "field name", msg: "error message", value: "<field input value>"}

      

The question is how to return the .msg parameter from the object? When i use

req.getValidationResult().then(function(result){
        if(!result.isEmpty()) {
            console.log(result.array());
            //return;
        } else {
            console.log('Validation Ok');
        }

      

the function returns an array. But I only need .msg.

+3


source to share


3 answers


The code should be as follows



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 {

      

+3


source


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

      



0


source


   request.getValidationResult().then(function(result) {
            if (result.array() != '') {
                response.render('route',{validation:result.array()})
                return;
              return;   
            } else {
               //valid
            }
        });

      

View:

 <% if(validation) {%>
<ul>
    <% for(var i = 0;i< validation.length;i++){ %>
    <li><%= validation[i].msg %></li>        
    <%}%>
</ul>
<% }%>

      

0


source







All Articles