I'm basically trying to delete an item in mongodb. But I just can't seem to pass the id into the url in the ajax call. Here's my code:
$(".delete-item").on('click', function(e, id) {
var deleteName = $('p.nameLink').text();
// Get ID first
$.ajax({
type: "GET",
url: "/items",
dataType: 'json',
})
.done(function(result) {
// searches mongodb for the clicked name and saves the result in the var
var newResult = $.grep(result, function(e){ return e.name === deleteName; });
var id = newResult[0]._id;
})
.fail(function (jqXHR, error, errorThrown) {
console.log(jqXHR);
console.log(error);
console.log(errorThrown);
});
// Then delete
console.log("ID to DELETE:", id); // I can see id
function itemDelete(id) {
$.ajax({
type: 'DELETE',
url: '/items/' + id,
dataType: 'json',
})
.done(function(result) {
console.log("ID to DELETE:", id); // Can't see the id
})
.fail(function (jqXHR, error, errorThrown) {
console.log(jqXHR);
console.log(error);
console.log(errorThrown);
});
}
itemDelete(id); // pass the id to the function
});
I'm just learning at the moment, so I could be going about this the wrong way. If anyone can help me out, I'd appreciate it.
The error message is:
DELETE https://www.someplace.com/items/undefined 500 (Internal Server Error)
type:'POST'insteadtype:DELETE''