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

140
Visualizações
I'm trying to build a simple To Do list in express, but I can't get my 'delete' button to work

I'm trying to make a simple to do list and I can't figure out the "delete" button. I don't get any errors, the buttons just doesn't do anything. I tried a lot of things but just can't seem to get it working. I'm not sure if the problem is with form action in the ejs file or with the app.delete in my index.js or both. Thank you in advance!

ejs file:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>To Do List</title>
</head>

<body>
    <h1>To Do List</h1>
    <form action="/todo" method="post">
        <input type="text" name="task">
        <button>Add</button>
    </form>
    <ul>
        <% for(let c of todos) { %>
            <li>
                <%= c.task %>
                    <form action="/todo/<%=c.id%>?_method=DELETE" method="POST"><button>Delete</button></form>
                    <%= c.id%>

            </li>
            <% } %>

    </ul>
</body>

</html>

index.js file:

const express = require('express');
const app = express();
const path = require('path');
const methodOverride = require('method-override');
const { v4: uuid } = require('uuid');
uuid();

app.use(methodOverride('_method'));
app.use(express.urlencoded({ extended: true }));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

let todos = [
    {
        id: uuid(),
        task: '1st task, testing'
    }
]

//TO DO INDEX
app.get('/todo', (req, res) => {
    res.render('todo', { todos });

})

//CREATE NEW TASK
app.post('/todo', (req, res) => {
    const { task, id } = req.body;
    todos.push({ task, id: uuid() });
    res.redirect('todo')

})

//DELETE TASK
app.delete('/todo/:id', (req, res) => {
    const { id } = req.body;
    todos = todos.filter(c => c.id !== id);
    console.log(todos);
    res.redirect('/todo')
})

app.listen(3000, () => {
    console.log('LISTENING ON PORT 3000')
})
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You have configured the delete route in your express app with method type DELETE that is app.delete(…), but in your html form

<form action="/todo/<%=c.id%>?_method=DELETE" method="POST">

The method is mentioned as POST. So express is not accepting it.

Form in html allows only GET and POST request.

You’ll need to trigger those delete api calls using fetch api or jquery or ajax.

Also i am not sure why that _method=DELETE is written in form action. According to me it not need there if your frontend is jinja/Django. If it’s something else then I’m not sure.

To get more insights about why it isn’t working, open the network tab in the browsers console and click your button. It should send a network call to you backend and you should be able to see there why backend isn’t doing anything for that call.

about 4 years ago · Juan Pablo Isaza Relatório

0

SOLVED IT

I just had to change const { id } = req.body; to const { id } = req.params;

about 4 years ago · Juan Pablo Isaza Relatório
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