Это мой топор ios Запрос на удаление:
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)
});
};
Я вызываю эту функцию здесь:
Пользователь. js
deleteAdmin() {
const user = this.state.usersInformation.find(
user => user.id.toString() === this.props.match.params.id
);
this.props.deleteAdmin(user.email);
}
В бэкэнде я получаю запрос здесь:
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" });
}
});
Обратите внимание на действие, у меня есть это в журнале:
// Записывает это: полученное электронное письмо внутри delete. Действие администратора: {email: "testDeleteButton@gmail.com "}
Обратите внимание на бэкэнд, у меня есть это в журнале:
Полученное письмо по маршруту deleteAdminAccountByEmail: {}
Итак тело запроса не получено в бэкэнде.
PS: Я следовал инструкциям в различных вопросах stackoverflow, включая этот one , изменив запрос ax ios следующим образом :
axios
.delete("http://localhost:3083/api/admin/deleteAdminAccountByEmail", { data:{email: email}})
Обратите внимание, я добавил ключ данных. Но я все равно получаю тот же результат.