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

309
Visualizações
Express DELETE 500 (Internal Server Error)

I'm trying to Remove my User from the system, but I'm getting error 500 (Internal Server Error)

I tried this with my Product and it still works fine. I didn't know where did I go wrong?

Below is my code

Router

router.get('/user-list/remove/:user_id', adminController.removeUser);

Service

async removeUser(user_id) {
    const userInDb = await this.userRepository.getById(user_id);
    if (userInDb) {
        await userInDb.destroy();
    } else {
        throw new Error();
    }
}

Controller

const removeUser = async (req, res) => {
try {
    const adminService = new AdminService();
    const data = req.params.user_id;
    await adminService.removeUser(data);
    res.send();
} catch (error) {
    res.status(500).json({ TRAVE: true, message: error });
}}

EJS

<a>
    <button class="deletebutton" value="<%= users[i].user_id %>">Delete</button>
</a>

<script type="text/javascript">
            $('.deletebutton').on("click", function (event) {
                event.preventDefault();
                var user_id = $(this).val();
                if (confirm("Are you sure you want to delete this data?")) {
                    $.ajax({
                        url: `/admin/user-list/remove/${user_id}`,
                        method: 'get',
                        data: { user_id: user_id },
                        success: function (data, textStatus, xhr) {
                            if (xhr.status == 200 || xhr.status == '200') {
                                $('.notification').html(`Delete done!`)
                            } else {
                                alert('Some error occurred try again');
                            }
                        },
                        error: function (response) {
                            error.html(`${response.responseJSON.message}`);
                        }
                    });
                }
            });
        </script>

And this is what I get from Console.log

GET http://localhost:5000/admin/user-list/remove/2?user_id=2 500 (Internal Server Error)

I tried with router.delete and method: 'delete' in the script but I get the same result. Where have I encountered the problem? I have a Product table, I have user_id as a foreign key, is it a problem?

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