I have got a delete function working using type: 'DELETE' this way and I am now trying to make an UPDATE function although I don't know if I'm doing this correctly, the code so far is the following:
ejs:
<a href="#" class="editEvent" data-id="<%= event._id %>">Edit</a></p>
js:
$(document).ready(function(){
$('.editEvent').on('click', editEvent);
});
function editEvent(){
var change = prompt('Change to:', '');
$.ajax({
type:'UPDATE',
url: '/events/update/'+$(this).data('id'),
data: change
}).done(function(response){
window.location.replace('/');
});
}
app.js:
app.post('/events/update/:id', function(req,res){
db.events.update({_id: ObjectId(req.params.id)}, {$set: {event_name: data}},function(err, result){
if(err){
console.log(err);
}
res.redirect('/');
});
});
So I want to update in MongoDB using $set and set the event_name to whatever the user inputs in the prompt(). The error on the consolole is:
UPDATE http://localhost:3030/events/update/5a959fdb9effb926a0594d90 400 (Bad Request)