1

I'm building a Node/Mongo/Express/Jquery application that's correctly processing a DELETE request but it does not refresh the page afterwards to update its contents.

I tried the answer posted for this question, but after including

req.method = 'GET'

inside the router.delete(url, callback) code block I was not getting different results.

CODE:

router.delete('/formula-list/:id', function(req, res){
  var db = req.db;
  var collection = db.get('formulas');
  var id = req.params.id;
  var query = { "_id": id };
  collection.findOneAndDelete(query);
  req.method = 'GET';
  res.redirect('/formula-list');
});

Again, the 'DELETE' request works just fine using a jQuery AJAX call, but I want the page to update to reflect that an item has been deleted from the database.

I've also tried the following to no avail:

router.delete('/formula-list/:id', function(req, res){
  var db = req.db;
  var collection = db.get('formulas');
  var id = req.params.id;
  var query = { "_id": id };
  collection.findOneAndDelete(query);
  collection.find({}, {}, function(e, docs){
  res.render('formula-list', {
    formulas: docs,
    title: 'Your Formulas',
    description: `List of saved user formulas from the formula generator`,
    ID: 'formula-list',
    keywords: 'formula generator, health kismet, nutraceutical formula  builder'
    });
  });
});

The idea here is that once I delete the item from the database I I re-send the collection to the URL to be re-displayed. I did this with and without adding req.method = 'GET' but this did not work either.

Thank you for your consideration.

Community
  • 1
  • 1
Jonathan Bechtel
  • 3,497
  • 4
  • 43
  • 73

1 Answers1

0

It depends on your front-end structure. If you are using single page application like Vue.js or React.js, what you can do is after ajax has been resolved, call a get query to fetch all the data from database again.

$ajax.delete('/formula-list/:id')
  .then(res => {
    $ajax.get('/formula-list')
     .then(res => {
       this.tableData = res.data
      })
   })

If you are doing SSR, maybe you can try window.location.reload(true) from front-end

Hansen W
  • 1,028
  • 1
  • 10
  • 17