Reputation: 67
I'm coding a react.js app like a blog where people can make posts, just like in the Medium platform. But, when my users try to make login in their account, my express server is returning an error that says that the app cannot return headers after they're already sent, but I don't know how to solve this thing...
My code:
app.post("/users/login", (req, res) => {
var ReqUsername = req.body.username;
var ReqPassword = req.body.password;
userModel.find((err, docs) => {
if(!err) {
console.log(docs)
docs.forEach(user => {
console.log(user)
var encryptedUsername = user.username;
var encryptedPassowrd = user.password;
var bytesUsername = CryptoJS.AES.decrypt(encryptedUsername, <SECRET KEY>);
var bytesPassword = CryptoJS.AES.decrypt(encryptedPassowrd, <SECRET KEY>);
var decryptedUsername = bytesUsername.toString(CryptoJS.enc.Utf8);
var decryptedPassword = bytesPassword.toString(CryptoJS.enc.Utf8);
if(decryptedUsername == ReqUsername) {
if(decryptedPassword == ReqPassword) {
res.json({
sucesso: true,
mensagem:"Login feito com sucesso",
userId: user.userId
}).status(202)
}
else {
res.json({
sucesso: false,
mensagem:"Por favor, confira as suas informações e tente login novamente"
}).status(404)
}
}
else {
res.json({
sucesso: false,
mensagem:"Por favor, confira as suas informações e tente login novamente"
}).status(404)
}
})
}
else {
res.json("Error on check user database").status(500)
}
})
})
After use post the login url, I'm always recieving this error:
2021-02-20T14:16:38.847139+00:00 app[web.1]: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
2021-02-20T14:16:38.847140+00:00 app[web.1]: at ServerResponse.setHeader (_http_outgoing.js:558:11)
2021-02-20T14:16:38.847140+00:00 app[web.1]: at ServerResponse.header (/app/node_modules/express/lib/response.js:771:10)
2021-02-20T14:16:38.847141+00:00 app[web.1]: at ServerResponse.send (/app/node_modules/express/lib/response.js:170:12)
2021-02-20T14:16:38.847141+00:00 app[web.1]: at ServerResponse.json (/app/node_modules/express/lib/response.js:267:15)
2021-02-20T14:16:38.847142+00:00 app[web.1]: at /app/main.js:79:35
2021-02-20T14:16:38.847142+00:00 app[web.1]: at Array.forEach (<anonymous>)
2021-02-20T14:16:38.847142+00:00 app[web.1]: at /app/main.js:65:21
2021-02-20T14:16:38.847143+00:00 app[web.1]: at /app/node_modules/mongoose/lib/model.js:4857:16
2021-02-20T14:16:38.847143+00:00 app[web.1]: at /app/node_modules/mongoose/lib/model.js:4857:16
2021-02-20T14:16:38.847144+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16
2021-02-20T14:16:38.847144+00:00 app[web.1]: at /app/node_modules/mongoose/lib/model.js:4880:21
2021-02-20T14:16:38.847144+00:00 app[web.1]: at /app/node_modules/mongoose/lib/query.js:4399:11
2021-02-20T14:16:38.847145+00:00 app[web.1]: at /app/node_modules/kareem/index.js:136:16
2021-02-20T14:16:38.847145+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:75:11)
2021-02-20T14:16:38.847146+00:00 app[web.1]: Emitted 'error' event on Function instance at:
2021-02-20T14:16:38.847146+00:00 app[web.1]: at /app/node_modules/mongoose/lib/model.js:4859:13
2021-02-20T14:16:38.847146+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16
2021-02-20T14:16:38.847147+00:00 app[web.1]: [... lines matching original stack trace ...]
2021-02-20T14:16:38.847147+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:75:11) {
2021-02-20T14:16:38.847147+00:00 app[web.1]: code: 'ERR_HTTP_HEADERS_SENT'
2021-02-20T14:16:38.847148+00:00 app[web.1]: }
I tried to take off some responses, but it doesn't work correctly. Can you guys help me to solve this error?
Upvotes: 0
Views: 1335
Reputation: 1651
Its not enough just to take off some responses
, you must have only one response per one request.
app.post("/users/login", (req, res) => {
var ReqUsername = req.body.username;
var ReqPassword = req.body.password;
userModel.find((err, docs) => {
if(!err) {
const user = docs.find(user => {
var bytesUsername = CryptoJS.AES.decrypt(user.username, SECRET KEY);
var decryptedUsername = bytesUsername.toString(CryptoJS.enc.Utf8);
return decryptedUsername === ReqUsername;
});
if(!user) {
res.json({
sucesso: false,
mensagem:"Por favor, confira as suas informações e tente login novamente"
}).status(404)
} else {
var bytesPassword = CryptoJS.AES.decrypt(user.password, SECRET KEY);
var decryptedUsername = bytesUsername.toString(CryptoJS.enc.Utf8);
if(decryptedPassword == ReqPassword) {
res.json({
sucesso: true,
mensagem:"Login feito com sucesso",
userId: user.userId
}).status(202)
} else {
res.json({
sucesso: false,
mensagem:"Por favor, confira as suas informações e tente login novamente"
}).status(404)
}
}
} else {
res.json("Error on check user database").status(500)
}
})
})
Upvotes: 2