5

I am trying to delete records based on ID. I am sending the ID via the get verb and acessing it from req.params.id. My code isn't working. What am I doing wrong?

//delete user
router.get('/deleteuser/:id', function(req, res){
    var db = req.db;
    db.users.remove({'_id':req.params.id}, function(err, docs) {
        if (err) return err;
        res.send(docs); // see results
    });
});

I realize I need to add "ObjectId" before req.params.id. However, concatenating is not working. I have this so far. Is there another way:

//delete user
router.get('/deleteuser/:id', function(req, res){
    var db = req.db;
    var objId = 'ObjectId("' + req.params.id + '")';
    console.log(objId);
    db.users.remove({"_id": objId}, function(err, docs) {
  //db.users.remove({"_id": ObjectId("4d512b45cc9374271b02ec4f")});  works in mongo console
        if (err) return err;
        console.log(docs);
        res.send(docs);
    });
});

This worked:

// db.ObjectId(req.params.id)

router.get('/deleteuser/:id', function(req, res){
    var db = req.db;
    db.users.remove({"_id": db.ObjectId(req.params.id)}, function(err, docs) {  //db.users.remove({"_id": ObjectId("4d512b45cc9374271b02ec4f")});
        if (err) return err;
        console.log(docs);
        res.send(docs);
    });
});
  • What data type is `_id`? If it is an `ObjectId`, then you need to give it an `ObjectID` object, which you can build from the base64 representation with the constructor: `new ObjectID(req.params.id)` – Brandon Jun 15 '14 at 20:03
  • Overlooking the fact that you use HTTP `GET` to delete an entity, which seems very evil, how do you know it isn't working? What happens? Do you get an error? Is the document still in the database after you delete it? What is the output of `docs`? – Brandon Jun 15 '14 at 21:26
  • Thinking about the use of `GET` for a moment, have you considered the effects of caching? Are you sure your requests are hitting your server? A print statement may help with this. – Brandon Jun 15 '14 at 21:27
  • It is hitting the server because if I delete by the firstname field it works. –  Jun 15 '14 at 21:34
  • It is also return n:0, so I can see it hasn't deleted anything. –  Jun 15 '14 at 21:50

2 Answers2

8

I accessed it using this:

db.ObjectId(req.params.id)
2

By Id can be delete by passing the Id in string form , no json needs be passed.

db.users.removeById(req.params.id, function(err, docs) {

shall be the line that shall come in.

If you want to maintain the JSON syntax you nead to do something like

db.users.remove({'_id': new ObjectID(req.params.id)}, function(err, docs) {

may do it. ObjectID will depend on the driver you are using.

saurshaz
  • 489
  • 5
  • 17
  • Neither of these worked. removeById and new Object return 'not defined.' –  Jun 15 '14 at 20:32
  • What does JSON have to do with this? – Brandon Jun 15 '14 at 21:24
  • The OP clarified that `_id` is a string, not an ObjectID – Brandon Jun 15 '14 at 21:25
  • https://github.com/kissjs/node-mongoskin#collectionremovebyidid- If you can let me know the driver you're using, i'll be able to help better. What I gave was for MongoSkin but every driver supports this as this is supported by mongoDb http://docs.mongodb.org/manual/applications/drivers/ – saurshaz Jun 16 '14 at 06:40