javascript - Mongodb updated but not showing in redirected page -
i using node/express web framework, here how structured delete action (it's nested in higher level route):
.delete((req, res) => { db.collection('collection-name').findoneanddelete({ topic_title: req.body.topic_title}, (err, item) => { if(err) return console.log(err); console.log('item deleted'); res.redirect('/'); });
client-side request:
$('.delete-button').click(function(){ $('.topic-title').text($('.topic-title').text()); if(confirm('are sure want delete ' + $('.topic-title').text() + '?')){ fetch('topics', { method: 'delete', headers: { "content-type" : 'application/json' }, body: json.stringify({ 'topic_title' : $('.topic-title').text() }) }); }
when hit delete button, page redirect index of entries in db.
the problem deleted entry still displays in redirected page, until page refreshed. i'm having problem update action.
what doing wrong here?
this same problem having myself although using jquery $.ajax call in similar way using fetch apu.
if think what's happening, sending request, separate rest of page, off server trigger delete
action within express. in turn deleting record database. fine, no problems there.
the problem occurs when delete
action finishes. redirecting /
route delete request being redirected not actual request page.
i couldn't figure out happening intially when encountered (earlier today actually). solution to, once knew database had been updated, remove element page thereby eliminating need page refresh / redirect. code have looks this:
$(function(){ $('.button').on('click', function(e){ var id = this.id; $.ajax({ url: '/', method: "delete", data: {item: id} }) .done(function(data){ $('#' + id).parent().remove(); }); }); });
there more elegant way handle removal of (in case) list item haven't got round looking @ yet.
also, might worth reconsidering fetch api if more personal project browser support seems very limited.
Comments
Post a Comment