Reputation: 89
I'm currently editing a back-end page, but I have a few problems.
I need a confirmation before I delete the Action
as you can see I tried this so far but my code is completely skipping the confirm()
and only redirects to the index.
Please take a look at the controller code below
delete(req, res, next) {
Action
.findOne(req.param('id'))
.then(({id}) => {
confirm('U staat op het punt een actie te verwijderen, klik op "ok" om hier mee door te gaan');
if(confirm()=== true) {
Action
.destroy(id)
.then(() => {
FlashService.setFlash(req, 'success', 'Action has been deleted')
res.redirect('/answer/index')
})
.catch(err => res.redirect('/answer/index'))
}
})
.catch(err => res.redirect('/answer/index'))
}
}
I also tried console.log
to see where my code is stranding but that would only give this error:
Unhandled rejection ReferenceError: confirm is not defined
edit
I tought it also might be usefull to maybe see the HTML so here you go as you can see the delete is called upon submit maybe I'm doing that extremly wrong but it seems to work besides the confirm mentioned before
<form action="/action/delete/<%= action.id %>" method="POST">
<button type="submit"
class="btn btn-sm btn-icon btn-pure btn-default on-default"
data-toggle="tooltip"
data-original-title="Verwijder"><i class="icon wb-trash" aria-hidden="true"></i>
</button>
</form>
EDIT 2.0
I kind of fixed the problem but still not really I made the following javascript for the delete it's a little basic but that isn't the problem please take a look
<script>
function Delete(){
let c = confirm('U staat op het punt een actie te verwijderen, klik op "ok" om hier mee door te gaan');
if (c){
console.log("done");
window.location.href = "/action/delete/<%= action.id %>"
}
else{
console.log("failed")
window.location.href = "/answer/show/<%= answer.id %>"
}
}
</script>
however, when I go to this page it says "action not defined" but I'm quite sure it is defined to be honest.
I don't really know what I'm doing wrong, any help would be nice
Upvotes: 0
Views: 256
Reputation: 9609
You cannot use the client side confirm
javascript on the server side. It does not exist. You should instead call confirm before sending request to the server.
Like so
<button onclick="delete()">Delete</button>
<script>
function delete() {
var confirm = confirm("Do you want to delete?")
if(confirm){
// Call server
}
}
</script>
Upvotes: 1