I've successfully implemented a delete function using type: 'DELETE', and now I'm attempting to create an UPDATE function. However, I'm unsure if I'm approaching this correctly. Here's the code I've written so far:
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('/');
});
});
I am looking to update in MongoDB using $set and assign the input from the user in the prompt() to the event_name. The error message appearing on the console is:
UPDATE http://localhost:3030/events/update/5a959fdb9effb926a0594d90 400 (Bad Request)