Express-Validator and req.getValidationResult()

Viewed 4186

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

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

The question is - how to return a .msg parameter from an 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 need only .msg.

4 Answers
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}`)
            })
        }
    }
})
Related