0

I've written a code to delete data from a mongodb, account made on mlab.com.

router.delete('/user/todo/:item', function(req, res, next){
console.log('Delete request called...');
var data = [{}];
MongoClient.connect(url, function(err, db) {
    if (err) throw err;     
    console.log('connection to mongo established...');
    console.log(req.params.item);
    var deleteitem = {$pull: {todo: {items:req.params.item}}};
    db.collection("users").update({profileID:req.user.profileID}, deleteitem , {multi:true});
    if (err) {console.log('something smrthing');}
    console.log('run to command...');
    res.json(data);
    console.log('responding json...');
    db.close();
    });
});

this is the code for router.js and below is code from html's script tag to send ajax request

$('li').on('click', function(){
            var item = $(this).text().replace(/ /g, "-");
            $.ajax({
                type: 'DELETE',
                url: '/user/todo/' + item,
                success: function(data){
                  //do something with the data via front-end framework
                    location.reload();
                }
              });
        });

The database looks like this

{
"_id": {
    "$oid": "595132feeba041141ceb8e5c"
},
"profileID": "1354404264673108",
"fullname": "Siddhant Srivastav",
"profilePic": "https://fb-s-c-a.akamaihd.net/h-ak-fbx/v/t1.0-1/p200x200/18839224_1337818779664990_2672385153328567253_n.jpg?oh=e1b3a2abfa1b4f9792e016eb15f404b2&oe=59DFD8A7&__gda__=1508193289_dabbf65c5a5a4d0f9f826815d5d13eba",
"__v": 0,
"todo": [
    {
        "item": "something"
    }
]

}

I want to delete <"item": "something">. This code doesn't do it. I recieve no error, all the console logs work fine.

  • Try `var deleteitem = {$pull: {todo: {item:req.params.item}}};` Changed `items` to `item` – s7vr Jul 03 '17 at 20:26
  • `.update()` is an "async" call. You are closing the database connection before this completes. You need to provide a callback or promise and only hang up the database connection "when this is resolved", which is "after" the actual operation is performed. – Neil Lunn Jul 04 '17 at 03:49

0 Answers0