gpt4 book ai didi

javascript - axios 删除后台未收到的req.body

转载 作者:行者123 更新时间:2023-12-02 21:47:11 26 4
gpt4 key购买 nike

这是我的 axios 删除请求:

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)
});
};

我在这里调用该函数:

User.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" });
}
});

在操作中注意,我在日志中有这样的内容:

// Logs this: received email inside delete Admin action is: {email: "testDeleteButton@gmail.com"}

在后端通知,我在日志中有这样的内容:

Received email inside deleteAdminAccountByEmail route: {}

所以后端没有收到req.body。
PS:我已按照各种 stackoverflow 问题中的说明进行操作,包括此 one通过像这样修改 axios 请求:

 axios
.delete("http://localhost:3083/api/admin/deleteAdminAccountByEmail", { data:{email: email}})

注意,我添加了数据 key 。但我仍然得到相同的结果。

最佳答案

尝试通过以下方式访问它:

req.params.email

关于javascript - axios 删除后台未收到的req.body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60225164/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com