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.– BrTkCa
Thanks @Lucascosta I took the test and it is running 2 times,I will try to make it have a single run now
– Thiago16Oli