Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
JS Full Stack REST API Empty JSON

I'm following this tutorial: https://www.youtube.com/watch?v=rMiRZ1iRC0A, around 1hr in where i'm developing an 'updating user' request.

However, once I do this, it appears i'm returning an empty JSON {} from my put request. It doesn't appear that I have any issues - I've actually gone straight to copying his github code and yet nothing.

Firstly, I have my verification through jwt, which appears to work. E.g if you pass an incorrect Header param, you will get "You are not authenticated". However - if you use the correct one, {} is returned. See below.

const jwt = require("jsonwebtoken")

const verifyToken = (req, res, next) => {
  const authHeader = req.headers.token;
  if (authHeader) {
    const token = authHeader.split(" ")[1];
    jwt.verify(token, process.env.JWT_SEC, (err, user) => {
      if (err) res.status(403).json("Token is not valid!");
      req.user = user;
      next();
    });
  } else {
    return res.status(401).json("You are not authenticated!");
  }
};

const verifyTokenAndAuthorization = (req, res, next) => {
  verifyToken(req, res, () => {
    if (req.user.id === req.params.id || req.user.isAdmin) {
      next();
    } else {
      res.status(403).json("You are not alowed to do that!");
    }
  });
};




module.exports = {verifyToken, verifyTokenAndAuthorization}

Otherwise, this is build into the router.put function within the user file. Please see below.

const { verifyToken, verifyTokenAndAuthorization } = require("./verifyToken");

const router = require("express").Router();


//UPDATE
router.put("/:id", verifyTokenAndAuthorization, async (req, res) => {
  if (req.body.password) {
    req.body.password = CryptoJS.AES.encrypt(
      req.body.password,
      process.env.PASS_SEC
    ).toString();
  }

  try {
    const updatedUser = await User.findByIdAndUpdate(
      req.params.id,
      {
        $set: req.body,
      },
      { new: true }
    );
    res.status(200).json(updatedUser);
  } catch (err) {
    res.status(500).json(err);
  }
});

    
module.exports = router;

And lastly, this is then called in the index.js, as per the standard app.use functions. I can confirm that my login/register API works, however if I attempt to change/update my user through a token as above, I'm not getting anything back.

E.g I use PUT localhost:5000/api/users/6177ede36c42e26f1d3c7b5f, and it returns {}

Can anyone see what i'm missing here!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda