-1
My program points ERR_HTTP_HEADERS_SENT which by what I understand, occurs when a response has already been sent by the server. I’m trying to create a controller method of a route that changes some user information. For this I made two other methods of the same controller, one that loads the user and the other authenticates. These two methods come before the method that changes user information, in that order: load -> authenticate -> alters. I don’t see where the ERR_HTTP_HEADERS_SENT comes from, since it points on the only line where a response is sent. I don’t have much experience with express. Here is the code:
controller.loadUser = async (req, res, next) => {
try {
await database.sync();
let userId = req.body.data.id;
const user = await User.findByPk(userId);
req.user = user;
}
catch (error) {
req.error = error;
}
finally {
next();
}
};
controller.authenticateUser = async (req, res, next) => {
if (req.error !== undefined)
next();
try {
await database.sync();
let {login: authLogin, password: authPassword} = req.body.data.authentication;
let {login, password} = req.user;
if (login === authLogin && password === authPassword)
next();
let error = new Error("User authentication failed.");
error.name = "UserAuthenticationError";
throw error;
}
catch (error) {
req.error = error;
}
finally {
next();
}
};
controller.updateUser = async (req, res, next) => {
if (req.error !== undefined)
next(req.error);
var user = req.user;
try {
await database.sync();
let newData = req.body.data.update;
for (let attribute of Object.keys(newData))
user[attribute] = newData[attribute];
user.save({
"fields": [
"username", "login", "password"
]
});
// Aqui é onde o error é apontado.
res.json({
"data": user
});
}
catch (error) {
next(error);
}
};
Here is the mistake:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:370:5)
at ServerResponse.setHeader (node:_http_outgoing:573:11)
at ServerResponse.header (/home/moccot/NetBeansProjects/secoar/node_modules/express/lib/response.js:771:10)
at ServerResponse.send (/home/moccot/NetBeansProjects/secoar/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/home/moccot/NetBeansProjects/secoar/node_modules/express/lib/response.js:267:15)
at controller.updateUser (/home/moccot/NetBeansProjects/secoar/src/api/controller/users.js:137:9)