Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
158
rated 0 times [  165] [ 7]  / answers: 1 / hits: 19980  / 3 Years ago, thu, august 26, 2021, 12:00:00

ERROR



(node:39756) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:561:11)
at ServerResponse.header (C:UserselegmOneDriveРабочий столanimflexapinode_modulesexpresslibresponse.js:771:10)
at ServerResponse.send (C:UserselegmOneDriveРабочий столanimflexapinode_modulesexpresslibresponse.js:170:12)
at ServerResponse.json (C:UserselegmOneDriveРабочий столanimflexapinode_modulesexpresslibresponse.js:267:15)
at C:UserselegmOneDriveРабочий столanimflexapiroutesauth.js:43:25
at processTicksAndRejections (internal/process/task_queues.js:95:5)




(Use node --trace-warnings ... to show where the warning was created)
(node:39756) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:39756) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.



Here's my code


router.post("/login", async (req, res) => {
try {
const user = await User.findOne({ email: req.body.email });
if (!user) {
res.status(401).json("Something went wrong!");
}
const bytes = CryptoJS.AES.decrypt(user.password,
process.env.SECRET_KEY);
const originalPassword = bytes.toString(CryptoJS.enc.Utf8);

if (originalPassword !== req.body.password) {
res.status(401).json("Something went wrong!");
}

res.status(200).json(user);
} catch (err) {
res.status(500).json(err)
}
});

module.exports = router;```

More From » node.js

 Answers
23

Reason



Error [ERR_HTTP_HEADERS_SENT] is an interesting error that is fired up when a server tries to send more than one response to a client.



Solution


router.post("/login", async (req, res) => {
try {
const user = await User.findOne({ email: req.body.email });
if(!user){
res.status(401).json("Something went wrong!");
}
const bytes = CryptoJS.AES.decrypt(user.password,
process.env.SECRET_KEY);
const originalPassword = bytes.toString(CryptoJS.enc.Utf8);

if(originalPassword !== req.body.password) {
res.status(401).json("Something went wrong!");
}

res.status(200).json(user);
} catch (err) {
res.status(500).json(err)
}
});

module.exports = router;

this is the proper way to handle the conditions in JS, here you can read more about error


[#50199] Wednesday, July 28, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shylaelisan

Total Points: 37
Total Questions: 94
Total Answers: 110

Location: Angola
Member since Tue, May 5, 2020
4 Years ago
;