Why isn't our object deleting? Node, Mongo, html

问题: Our objects are built by the users passing info through a form. We can successfully create and post objects just fine, but we can not delete them. I've tried many differ...

问题:

Our objects are built by the users passing info through a form. We can successfully create and post objects just fine, but we can not delete them.

I've tried many different routes/calls for this but none of them have been successful.

Here is our HTML button to trigger the delete call in Node:

   <form  id='deleteMe' name='deleteMe' method='delete' action=''deleteMe'> 
    <input name='_method' value='delete'>
   <button type='submit'> delete </button></form>

here is the express route for the deletion itself.

router.post('/deleteMe', function (req, res, next) {
    const _id = req.body.id

    mongoose.connect(URL, function (err, db) {
        assert.equal(null, id)
        db.collection('spots').deleteOne({ "_id": objectId(_id) }, function (err, result) {
            console.log('op')
            assert.equal(null, err);
            db.close();
        })
    });
})

Any help would be so appreciated.


回答1:

You are using delete method in you form, which is correct and post method on you express server, try using router.delete instead of router.post, like this,

router.delete('/deleteMe', function (req, res, next) {
    const _id = req.body.id

    mongoose.connect(URL, function (err, db) {
        assert.equal(null, id)
        db.collection('spots').deleteOne({ "_id": objectId(_id) }, function (err, result) {
            console.log('op')
            assert.equal(null, err);
            db.close();
        })
    });
})

Also on your form you have a syntax error on action attribute it should be action='deleteMe'


回答2:

You have an extra quote in the html at

action=''deleteMe'

  • 发表于 2018-09-02 01:00
  • 阅读 ( 213 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除