'how to send ObjectID with ajax to backend (Node.JS) [closed]

font-end code(HTML, Javascript) I want to make delete button but i can't send ObjectID with ajax

var retName = $(this).data("row-id");
    $.ajax({
          type: "POST",
          url: "/delete",
          data: 
            {'id' : ObjectId(retName)}
          ,
          success: function() {

          },
          error: function(textstatus, errorThrown) {
            alert('text status' + textstatus + ', err ' + errorThrown);
          }
        });

back-end code(Node.js)

app.post('/delete', function(require, response) {
  db.open(function(error, db) {
  db.collection('infodb', function(error, collection) {
  var doc = require.body;
  console.log(ObjectId(doc))
  collection.remove(doc, function() {
    db.close();
  });
});
  });
});

thank you very much



Solution 1:[1]

I am not sure why are you using "POST" method to delete data. Why cant you just define delete method and give a proper name to api. Because API name can not be a verb and here delete is the verb. You can define your code like this and i am going to give this api name as "user". you can change it

var retName = $(this).data("row-id");
    $.ajax({
          type: "DELETE",  //Delete method here 
          url: '/user/'+retName ,
          success: function(data) {
console.log(data);
          },
          error: function(textstatus, errorThrown) {
            alert('text status' + textstatus + ', err ' + errorThrown);
          }
        });

and in node js

app.delete('/user/{id}', function(req, response) {
 console.log(req.params.id); //this will give you id
  db.open(function(error, db) {
  db.collection('infodb', function(error, collection) {
  //var doc = require.body;
  console.log(ObjectId(doc))
  collection.remove(doc, function() {
    db.close();
  });
});
  });
});

Solution 2:[2]

I think in your NodeJS, it should be

app.post('/delete', function(request, response) {
  db.open(function(error, db) {
    db.collection('infodb', function(error, collection) {
      var doc = request.body.id;
      console.log(doc)
      collection.remove(doc, function() {
        db.close();
      });
    });
  });
});

since you want the parameter id.

Also since it is already the object ObjectId I don't understand why you would get the id of that object.

If this doesn't work, did you try passing another variable instead of your ObjectId?

Solution 3:[3]

You cannot send body params in delete method. Instead of sending id in body params, use url params or query parameters to send id to api method.

app.post('/delete/:id', function(request, response) { 
  db.open(function(error, db) {
    db.collection('infodb', function(error, collection) {
      var doc = ObjectId(request.params.id); 
      console.log(ObjectId(doc)) 
      collection.remove({_id:doc} , function() {
        db.close();
      });
    });
  });
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kumawat
Solution 2 Vishnu
Solution 3