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

93
Visualizações
POST method's response is doing nothing . cant res.json() nor res.render()
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');

const JWT_SECRET = require('../../config/keys').JWT_SECRET;
const userModel = require('../../models/User');

const postChangePassword = async (req, res) => {
    let isChanged = true;
    const { token, newPassword } = req.body;

    try {
        const verified = jwt.verify(token, JWT_SECRET);
        const _id = verified.id;
        const hashedPassword = await bcrypt.hash(newPassword, 8);
        await userModel.updateOne({ _id }, { $set: { password: hashedPassword } });
    } catch (error) {
        isChanged = false;

        console.log(error);
    }

    res.render('change-password', { isChanged });
    console.log('wow');
};

module.exports = postChangePassword;

My controller is like that for using app.post("/change-password", postChangePassword). But res render is unresponsive. It does not giving error but it's not working even I try res.json or redirect. The request is coming from fetch() method another in the app and the other codes is working well except res.something.

And here is my POST code where request comes from

  <script>


        async function postToken(event) {

            event.preventDefault();
            const password = document.getElementById('password').value
            const token = localStorage.getItem('token')

            const result = await fetch('/authUser/change-password', {
                method: 'POST',
                mode: 'cors',

                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },

                body: JSON.stringify({

                    newPassword: password,
                    token: token
                }),

            })
        }

        const form = document.getElementById('form')
        form.addEventListener('submit', postToken)
    </script>
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