R. Luthra
R. Luthra

Reputation: 33

Sending a DELETE request with EJS using Mongoose

So I have already made a Restful API with node and everything works but I am trying to add EJS to it so I can use HTML&CSS, I implemented GET and POST just fine but I am tripping up on DELETE.

Here is my code in my router to delete

listRouter.delete('/:id', 
    function(req, res) {
        req.list = list;
        req.list.remove(function(err){
            if (err)
                res.status(500).send(err);
            else
                res.redirect('/')
        });
});

and here's my EJS for deletion

<form method="DELETE" action="/:id">
      <button type="submit">Delete</button>
</form>

and this is the error I receive when I press the button

{
message: "Cast to ObjectId failed for value ":id" at path "_id"",
name: "CastError",
kind: "ObjectId",
value: ":id",
path: "_id"
}

The thing is though the same exact code works if it's modified for JSON so I don't know if its EJS or my Javascript.

Thanks

Upvotes: 3

Views: 10589

Answers (3)

Rehman Ali
Rehman Ali

Reputation: 1

Through HTML or ejs or other Views engine, You can only send the POST and GET request. However, there is a NPM Package name Method-override.

npm i method-override;

Now require it in App.js or inde.js file. and use app method for it.

var methodOverride = require('method-override');

app.use(methodOverride('_method'));

and write the

<form method='POST' action="link?_method=DELETE>

Upvotes: 0

I am not englishman
I am not englishman

Reputation: 69

giving up use of method-override can be solution

I used different url to solve this.

<form action="/quake/forum/update/<%= post._id %>?_method=put" method="post">

and

<form action="/quake/forum/delete/<%= post._id %>?_method=delete" method="post" class="d-inline">

and router

main router

app.use('/quake/forum',forumRouter); //this is for just making sure explaination clear

sub router (forumRouter)

router.post('/delete/:id', function (req, res) {

and

router.post('/update/:id', function (req, res) {

Upvotes: 1

Faisal Arkan
Faisal Arkan

Reputation: 139

i think html5 just support post and get in method form attribute

however, in my case if i don't using form for submit, here example

example in html or front end

<a class="btn btn-raised btn-danger" href="/admin/dashboard/detele/<%= data.userId %>">Delete</a></td> 
<!-- The href needs to point at a link where data.userId is shown. Hence you need the = after <% for ejs to show the variable. -->

In app.js for the url delete

app.get('/admin/dashboard/detele/:id', users.deleteUser);

in express users.js

exports.deleteUser = function(req, res) {

    users.findOneAndRemove({
        userId: req.params.id
    }, function(err, user) {

        if (err) throw err;

        console.log("Success");

    });

    res.redirect('/admin/dashboard');

}

don't forget creating mongo model for mongoose

var skema = new mongo.Schema({

    name: String,
    email: String,
    password: String,
    date: {
        type: Date,
        default: Date.now
    },
    admin: Boolean
});


var users = mongo.model('accounts', skema);

i using EJS here, hope it's help you

Upvotes: 3

Related Questions