最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sending a DELETE request with EJS using Mongoose - Stack Overflow

programmeradmin5浏览0评论

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

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

Share Improve this question asked Sep 24, 2016 at 2:39 R. LuthraR. Luthra 331 gold badge1 silver badge4 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

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

  • more helpful link1
  • more helpful link2

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) {

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>
发布评论

评论列表(0)

  1. 暂无评论