Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

111
Vistas
Getting an error on requesting a user details update as Cannot read properties of undefined (reading 'id')

This is my update request for updating a user, but the title is the output that am getting

router.put("/:id", verifyTokenAndAuthorization, async (req, res) => {
  if (req.user.password) {
    req.body.password = CryptoJS.AES.encrypt(
      req.body.password,
      process.env.PASS_SEC
    ).toString();
  }
  try {
    const updatedUser = await User.findByIdAndUpdate(
      req.user.id,
      {
        $set: req.body,
      },
      { new: true }
    );
    res.status(200).json(updatedUser);
  } catch (err) {
    res.status(500).json(err);
  }
});

But the request doesn't go past

const verifyTokenAndAuthorization = (req, res, next) => {
  verifyToken(req, res, () => {
    console.log("req recived for login");
    if (req.user.id === req.params.id || req.user.isAdmin) {
      console.log("req went past verification");
      next();
    } else {
      res.status(403).json("You are not allowed to access!");
    }
  });
};

the problem is in before the output req went past verification

This is printed in my console

req recived for login Error = "Cannot read properties of undefined (reading 'id')" at E:\E-com Project\ver-one-zero-one-api\routes\verifyToken.js:20:18

7 months ago · Santiago Gelvez
Responde la pregunta
Encuentra empleos remotos