Home > Net >  I'm trying to build a simple To Do list in express, but I can't get my 'delete'
I'm trying to build a simple To Do list in express, but I can't get my 'delete'

Time:03-16

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')
})

CodePudding user response:

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.

CodePudding user response:

SOLVED IT

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

  • Related