David
David

Reputation: 21

(Express Validator) req.check doesn't work

I use Express Validator for a update form. But when I run my function, I have this error.

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

I check all my code and i think a little response with POST

{
    "errors": [
        {
            "location": "params",
            "param": "password",
            "msg": "password is required"
        },
        {
            "location": "body",
            "param": "confirmPassword",
            "msg": "password and passwordConfirm are not equals",
            "value": "amazingPassword"
        }
    ]
}

I don't understand why the location on password is in "params" ?? All my variables are in location body, but password and confirmPassword are in location params.

I don't know if i'm clear. Express Validator is ok, the problem is the crash of node who have sent a response to the client.

My app use body parser, a duplicate of var password, it is possible ?

My POSTMAN configuration: My POSTMAN configuration

Thank you for your help :)

Upvotes: 1

Views: 2260

Answers (1)

Andrey Shcherbakov
Andrey Shcherbakov

Reputation: 138

I think you are using this:

const { check } = require('express-validator/check');

And this function check everything: body, cookies, headers, params, query. According to your screenshot you are not sending password, so check doesn't know where it should be.

If you are expecting password in body, use

const {body, query, param} = require('express-validator/check');
body('password').exists()

Second error simply says that you already sent headers(validation errors) and trying to do it one more time(maybe sending some error in catch, dunno, share your controller code)

Upvotes: 1

Related Questions