Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

307
Views
No se pueden establecer encabezados después de que se envían al cliente en forma expresa

Me registré con éxito para insertar un registro en mi mongodb, pero cuando intento iniciar sesión, se produce un error en la línea "! usuario && res.status (401). json ("Nombre de usuario incorrecto"); " eso

 Cannot set headers after they are sent to the client at new NodeError (node:internal/errors:372:5) at ServerResponse.setHeader (node:_http_outgoing:576:11) at ServerResponse.header (/home/hahp1908/EShopAPI/node_modules/express/lib/response.js:794:10) at ServerResponse.send (/home/hahp1908/EShopAPI/node_modules/express/lib/response.js:174:12) at ServerResponse.json (/home/hahp1908/EShopAPI/node_modules/express/lib/response.js:278:15) at /home/hahp1908/EShopAPI/routes/auth.js:42:25 at processTicksAndRejections (node:internal/process/task_queues:96:5) { code: 'ERR_HTTP_HEADERS_SENT' }
 const router = require('express').Router(); const User = require("../models/User"); const CrytoJS = require("crypto-js"); router.post('/login', async (req, res) => { try{ const user = await User.findOne( { username: req.body.user_name } ); !user && res.status(401).json("Wrong User Name"); const hashedPassword = CryptoJS.AES.decrypt( user.password, process.env.PASS_SEC ); const originalPassword = hashedPassword.toString(CryptoJS.enc.Utf8); const inputPassword = req.body.password; originalPassword != inputPassword && res.status(401).json("Wrong Password"); res.status(200).json(user); }catch(err){ res.status(500).json(err); } }); module.exports = router
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe finalizar la ejecución de la función cuando llame a res.status().json(); de lo contrario, simplemente continuará y volverá a establecer res.status().json(). Esto está causando el error.

Modificar a algo como:

 const router = require('express').Router(); const User = require("../models/User"); const CrytoJS = require("crypto-js"); router.post('/login', async (req, res) => { try{ const user = await User.findOne( { username: req.body.user_name } ); if(!user) return res.status(401).json("Wrong User Name"); const hashedPassword = CryptoJS.AES.decrypt( user.password, process.env.PASS_SEC ); const originalPassword = hashedPassword.toString(CryptoJS.enc.Utf8); const inputPassword = req.body.password; if(originalPassword != inputPassword) return res.status(401).json("Wrong Password"); return res.status(200).json(user); }catch(err){ return res.status(500).json(err); } }); module.exports = router
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!