I'm trying to Remove my User from the system, but I'm getting error 500 (Internal Server Error)
I tried this with my Product and it still works fine. I didn't know where did I go wrong?
Below is my code
Router
router.get('/user-list/remove/:user_id', adminController.removeUser);
Service
async removeUser(user_id) {
const userInDb = await this.userRepository.getById(user_id);
if (userInDb) {
await userInDb.destroy();
} else {
throw new Error();
}
}
Controller
const removeUser = async (req, res) => {
try {
const adminService = new AdminService();
const data = req.params.user_id;
await adminService.removeUser(data);
res.send();
} catch (error) {
res.status(500).json({ TRAVE: true, message: error });
}}
EJS
<a>
<button class="deletebutton" value="<%= users[i].user_id %>">Delete</button>
</a>
<script type="text/javascript">
$('.deletebutton').on("click", function (event) {
event.preventDefault();
var user_id = $(this).val();
if (confirm("Are you sure you want to delete this data?")) {
$.ajax({
url: `/admin/user-list/remove/${user_id}`,
method: 'get',
data: { user_id: user_id },
success: function (data, textStatus, xhr) {
if (xhr.status == 200 || xhr.status == '200') {
$('.notification').html(`Delete done!`)
} else {
alert('Some error occurred try again');
}
},
error: function (response) {
error.html(`${response.responseJSON.message}`);
}
});
}
});
</script>
And this is what I get from Console.log
GET http://localhost:5000/admin/user-list/remove/2?user_id=2 500 (Internal Server Error)
I tried with router.delete and method: 'delete' in the script but I get the same result. Where have I encountered the problem?
I have a Product table, I have user_id as a foreign key, is it a problem?