axios删除后端未收到req.body

Posted

技术标签:

【中文标题】axios删除后端未收到req.body【英文标题】:Axios Delete req.body not received in the backend 【发布时间】:2020-05-30 04:25:44 【问题描述】:

这是我的 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)
     );
 ;  

我在这里调用该函数:

用户.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" );
      
    );

注意在行动中,我在日志中有这个:

// 记录以下内容:在删除管理操作中收到电子邮件: 电子邮件:“testDeleteButton@gmail.com”

注意在后台,日志里有这个:

在 deleteAdminAccountByEmail 路由中收到电子邮件:

所以后端没有收到req.body。PS:我已经按照各种***问题中的说明,包括这个one,通过修改axios请求如下:

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

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

【问题讨论】:

@ponury-kostek 不,我知道 axios 中允许实体主体。所以这个问题无关紧要 【参考方案1】:

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

req.params.email

【讨论】:

以上是关于axios删除后端未收到req.body的主要内容,如果未能解决你的问题,请参考以下文章

删除时为空(req.body)(角度)

Express.js 中删除 req.body 时的空对象

使用 Mongoose 批量删除

无法使用 upsert 删除键

如何将一组数据从客户端传递到 axios post 请求的 req.body

Req.Body 是 Angular 2 表单提交 + Bodyparser 上的空对象