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

153
Views
¿Por qué esta identificación de usuario no sale de req.user?

Este es mi código para obtener información del usuario.

 router.post("/getuser", fetchuser, async (req, res) => { try { userId = req.user.id; const user = await User.findById(userId).select("-password"); res.send(user); } catch (error) { console.error(error.message); res.status(500).send("Internal Server Error"); } });

y este es el código para el buscador de middleware

 const fetchuser = async (req, res, next) => { const token = req.header('auth-token'); if (!token) { res.status(401).send({ error: "Please authenticate using a valid token" }) } try { const data = jwt.verify(token, process.env.SECRET); console.log(data); req.user = data.user; next(); } catch (error) { res.status(401).send({ error: "Please authenticate using a valid token" }) } };

Obtengo la identificación de usuario en console.log, pero cuando intento obtener la identificación de usuario en router.post, no puedo obtener la información del usuario. Aquí está el resultado que estoy obteniendo.

 Server is running on port 5000 Connected to database { id: '61e98c45a9d8818292b38505', iat: 1642743501 } Cannot read properties of undefined (reading 'id')

Por favor, ¿alguien puede decirme qué tiene de malo esto?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

 router.post("/getuser", fetchuser, async (req, res) => { try { const params = JSON.parse(req) userId = params.id; const user = await User.findById(userId).select("-password"); res.send(user); } catch (error) { console.error(error.message); res.status(500).send("Internal Server Error"); } });
about 4 years ago · Juan Pablo Isaza Report

0

Su código funciona bien, solo tiene que cambiar esta línea en el middleware fetchuser

req.user = data.user

a

req.user = data

y su código funciona como se esperaba ...

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!