I have a simple fetch that happens after clicking a button to like a post:
function like(post_id){
fetch(`/like/${post_id}`);
}
router.get('/like/:post_id/', authController.isLoggedIn, async (req, res, next) => {
try {
//liking
req.flash("flash", 'You liked the post!');
return res.redirect('back');
}
catch{
req.flash("flash", 'Like failed :(');
return res.redirect('back');
}
});
In theory this should do its thing and then redirect back to the same page with the flash message.
Instead of doing that it loads the page with the fash message as afetch request I never called for!
It should reload the page to act/terrain/... not load it on the side. What's my problem? Do I need to end the fetch somehow before redirecting?
The mistake is using fetch in the first place. This is how I got it to work:
function like(post_id){
document.body.innerHTML += `<form id="jsForm" action="/like/${post_id}" method="GET"><input type="hidden" name="a" value="a"></form>`;
document.getElementById("jsForm").submit();
}