后端未收到Axios删除要求的主体

问题描述 投票:0回答:1

这是我的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: "[email protected]"}
  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" });
      }
    });

注意事项,我在日志中有此内容:

//记录此:删除管理员操作中收到的电子邮件是:{email:“ [email protected]”}

后端通知,我在日志中有这个:

在deleteAdminAccountByEmail路由内收到的电子邮件:{}

因此请求正文在后端未收到。PS:我通过修改axios请求来遵循包括以下one在内的各种stackoverflow问题中的说明:

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

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

javascript node.js express axios
1个回答
1
投票

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

req.params.email
© www.soinside.com 2019 - 2024. All rights reserved.