This is my axios Delete request:
adminActions.js
export const deleteAdmin = (email) => dispatch => {
console.log('receivedEmail in deleteAdmin action is: ', email)
// Logs this: received email inside delete Admin action is: {email: "testDeleteButton@gmail.com"}
return axios
.delete("http://localhost:3083/api/admin/deleteAdminAccountByEmail", {email: email})
.then(res => {
console.log("inside POST http://localhost:3083/api/admin/deleteAdminAccountByEmail");
console.log("res.data.admin", res);
if (res.data.success) {
console.log('ADMIN HAS BEEN SUCCESSFULLY DELETED')
}
return (true)
})
.catch(err => {
console.log("Admin Deletion has failed. ERROR: ");
console.log(err);
return(false)
});
};
I am calling that function here:
User.js
deleteAdmin() {
const user = this.state.usersInformation.find(
user => user.id.toString() === this.props.match.params.id
);
this.props.deleteAdmin(user.email);
}
In the backend, I am receiving the request here:
admin_routes.js
// @route DELETE api/admin/deleteAdminAccountByEmail
// @desc deleteAdminAccount By email
// @access Private
router.delete("/deleteAdminAccountByEmail", isLoggedIn, function(req, res) {
logger.notice("INSIDE ROUTE: deleteAdminAccountByEmail");
console.log('Received email inside deleteAdminAccountByEmail route: ')
console.log(req.body)
// Logs this :Received email inside deleteAdminAccountByEmail route: {}
let loggedInAdmin = {
token: req.user.token,
id: req.user.id,
email: req.user.email,
name: req.user.name,
role: req.user.role
};
if (loggedInAdmin.role === "super_user") {
logger.notice("The logged-in admin's role is super_user.");
// Can delete admin account 'administrateur','operateur_sav','agent_support_tel','agent_magasin'
adminService.deleteAdminByEmail(req.body.email, req, res);
} else if (loggedInAdmin.role === "administrateur") {
logger.notice("The logged-in admin's role is administrateur.");
adminService.isSuper_UserByEmail(req.body.email).then(isSuper_User => {
logger.info(
"req.params.email: INSIDE RETURNE PROMISE ",
req.params.email
);
if (isSuper_User) {
logger.debug(
"adminService.isSuper_User(req.params.email): ",
isSuper_User
);
res.status(200).json({
unauthorized: "The operation is unauthorized"
});
} else {
//UserToBeDeleted is not Super_User. Check if it is administrateur.
adminService
.isAdministrateurByEmail(req.body.email)
.then(isAdministrateur => {
logger.info(
"req.params.email: INSIDE RETURNE PROMISE ",
req.params.email
);
if (isAdministrateur) {
logger.debug(
"adminService.isAdministrateur(req.params.email): ",
isAdministrateur
);
res.status(200).json({
unauthorized: "The operation is unauthorized"
});
} else {
//UserToBeDeleted is neither Admin nor Super_User. So the administrateur can delete it.
adminService.deleteAdminByEmail(req.body.email, req, res);
}
});
}
});
} else {
// The logged-in admin's role is neither super_user nor administrateur. Deletion of admin accounts is forbidden.
logger.notice(
"The logged-in admin's role is neither super_user nor administrateur. Deletion of admin accounts is forbidden."
);
res
.status(401)
.send({ success: false, tag: "Unauthorized to delete admin accounts" });
}
});
Notice in the action, I have this in the log:
// Logs this: received email inside delete Admin action is: {email: "testDeleteButton@gmail.com"}
Notice in the backend, I have this in the log:
Received email inside deleteAdminAccountByEmail route: {}
So the req.body is not received in the backend.
PS: I have followed the instructions in various stackoverflow questions including this one by modifying the axios request like this:
axios
.delete("http://localhost:3083/api/admin/deleteAdminAccountByEmail", { data:{email: email}})
Notice, I added the data key. But I still get the same result.