Delete method can't get req.params!

Method delete can’t see req.params.
I have been looking for information about req.params a couples day. And found nothing.
Maybe somebody could help with finding resolve?

taskController.js

const TodoTask = require("../models/todoTask");

const deleteTask = async (req, res) => {
    try {
        const id = req.params.id; 

       
        << Can't get ID  or req.params.id  >>     


        console.log(id);
        if (!id) res.status(404).send("No id for remove");
        let result = await TodoTask.findByIdAndDelete(id);
        if (!result) res.status(404).send("No result");
        res.redirect('/');
    } catch (e) {
        res.status(500).send(e);
    }
}


module.exports = {
    deleteTask,
}

manipulateRouter.js

const express = require("express");
const router = express.Router({mergeParams:true});
const {
    deleteTask,
} = require("../controllers/tasksController");

router.delete("/delete/:id", deleteTask);

module.exports = router;

models todoTask.js

const mongoose = require("mongoose");

const taskSchema = new mongoose.Schema({
    content: {
        type: String,
        required: true
    },
    date: {
        type: String,
        default: Date.now()
    }
})

const Task = mongoose.model('Todotask', taskSchema);

module.exports = Task;

todo.ejs

<body>

<div class="todo-container"><h2>To-Do List</h2>
    <div class="todo">
        <form action="/" method="POST" class="todo-header">
            <!--suppress HtmlFormInputWithoutLabel -->
            <input type="text" name="content">
            <button type="submit">
                <span class="fas fa-plus">ADD</span>
            </button>
        </form>
        <ul class="todo-list">
            <% todoTasks.forEach(details => { %>
                <li class="todo-list-item">
                    <div class="todo-list-item-name"><%= details.content %></div>
                    <a href="/edit/<%= details._id %>" class="edit">
                        <span class="far fa-edit"></span>
                    </a>
                    <a href="/delete/<%= details._id %>" class="remove">
                        <span class="fas fa-times"></span>
                    </a>
                </li>
            <% }) %>
        </ul>
    </div>
</div>
</body>

index.js

const express = require("express");
const path = require("path");
const mongoose = require("mongoose");
const app = express();
const dotenv = require('dotenv');
const tasksRouter = require("./routes/manipulateRouter");

dotenv.config();

mongoose.connect(process.env.DB_CONNECT)
    .then(() => {
        app.listen(3000, () => console.log('Server Up and running'));
        // console.log('hello');
    })
    .catch(err => console.log(err))


app.use('/static', express.static(path.join(__dirname, 'public')));

app.use(express.urlencoded({extended: true}));

app.set("view engine", "ejs");

app.use('/', tasksRouter);

check if the route is correctly set up and the request URL includes the task ID

Yes! It is OK! I think I know what’s problem in :). HREF send GET request and I need DELETE request. I need to change method of the request to delete. Then I’ll be able to get req.params. Cause if I set method GET like this: router.get then I can get req.params.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.