generates error Can’t set headers after they are sent,is running 2 times

Asked

Viewed 742 times

0

He’s running the contents of the validator 2 times, I’m new to Node.js and I don’t know what’s going on.

When Validator returns an error to API for, when I have no return of an error the api works normal.

function alterar(req,res){

    validator.validaValores(req.body,(err,data) => {
        if(err){
            console.log(err)
            return res.status(500).json(err)
        }else{
            console.log("Teste")
            repository.alterar(req.body, (err,data) => {
                if(err){
                    return res.status(500).json(err)
                }else{
                    res.status(200).json({mesage: 'Alterado com sucesso!'})
                }    
            })
        }      
    })
}

This is the mistake, both of you console.log() that I give in the code appear there, but should appear only the one with the error message.

{ error: 'Invalid data: '!' }

Testing _http_outgoing.js:489 throw new Error('Can’t set headers after they are sent.'); ^

Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:489:11)
    at ServerResponse.setHeader (_http_outgoing.js:496:3)
    at ServerResponse.header (/home/thiago/projeto/projeto/projeto_api/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (/home/thiago/projeto/projeto/projeto_api/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/home/thiago/projeto/projeto/projeto_api/node_modules/express/lib/response.js:267:15)
    at repository.alterar (/home/thiago/projeto/projeto/projeto_api/src/core/valores/teste/testeController.js:39:37)
    at sql.request.input.input.input.input.input.execute (/home/thiago/projeto/projeto/projeto_api/src/core/valores/teste/testeRepository.js:48:17)
    at /home/thiago/projeto/projeto/projeto_api/node_modules/mssql/lib/main.js:1632:20
    at Request.userCallback (/home/thiago/projeto/projeto/projeto_api/node_modules/mssql/lib/tedious.js:1137:61)
    at Request.callback (/home/thiago/projeto/projeto/projeto_api/node_modules/tedious/lib/request.js:39:27)
[nodemon] app crashed - waiting for file changes before starting...
  • It doesn’t make sense for him to enter the two in the same shot, maybe he’s running more than once, look at this. And he doesn’t need the return in case of error, you can remove it. And the error might be being displayed as when you do .json(err), the content must be a valid json, maybe it’s just a string, or code, etc.

  • Thanks @Lucascosta I took the test and it is running 2 times,I will try to make it have a single run now

1 answer

0


The problem was that I was running twice the function of mine validator so I solved it this way:

function alterar(req,res){
try{
    validator.validaValores(req.body,(err) => {
        if(err){
            throw err
        }else{
            repository.alterar(req.body, (err,data) => {
                if(err){
                    throw err
                }else{
                    res.status(200).json({mesage:'Alterado com sucesso!'})    
                }    
            })
        }      
    })
}catch(err){
    return res.status(500).json(err)
}

}

I switched out the return for throw using try catch to capture the error and end the function alterar leaving only two returns.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.