【问题标题】:node js async/await : why i can't get the data ? req.bodynode js async/await:为什么我无法获取数据?请求正文
【发布时间】:2019-03-13 01:50:23
【问题描述】:

我正在 Node.js 学习 async/await 来制作一个安静的 api,但我在 PUT 和 PATCH 方法中遇到了问题,对于 req.body,它无法显示我想要的数据

这里是代码:controllers/users

 replaceUser: async (req, res, next) => {
    //enforce that req.body must contain all the fields
    const { userId } = req.params;
    const  newUser  = req.body;
    // const result = await User.findByIdAndUpdate(userId, newUser, {new: true}).exec();
    // console.log(result)
    console.log(newUser)
    console.log(userId)

    // res.status(200).json(result);
    // console.log(userId, newUser)
},

这个路由器代码:

router.route('/:userId')
.get(UsersController.getUser)
.put(UsersController.replaceUser)
.patch(UsersController.updateUser)

当我启用 mongoose 调试时,只有findone 功能处于活动状态,并且此方法适用于GETPOST

我使用:

    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "express-promise-router": "^3.0.3",
    "mongoose": "^5.3.1",

我已经在我的 app.js 中设置了 bodyparser 中间件.. 但仍然不适用于 PATCH 和 PUT 方法:(

请帮助我。我被困住了。谢谢你

【问题讨论】:

  • 对不起,我看错了你的代码......你在使用 expressjs 吗?你有没有包括身体解析器? stackoverflow.com/questions/11625519/…
  • 是的,我已经安装了一个body-parser..抱歉信息不完整
  • @boogeywoogy 你能记录下req.paramsreq.body 的值吗
  • 这很酷@boogeywoogy - 现在只是让两个答案都变得多余了:p
  • 在这种情况下,问题可能出在客户端代码中

标签: javascript node.js api async-await


【解决方案1】:

我遇到了同样的问题,并尝试了 stackoverflow 中的所有答案,但没有任何效果。最后我发现我正在以文本形式发送数据,它应该是 json 格式。所以,如果你确定你所做的一切都是正确的,请检查 postman 中的 body 部分并将数据类型更改为 json。

【讨论】:

    【解决方案2】:

    [已解决] 最后我从 req.body 得到了我的数据.. 问题是.. 我忘记在邮递员的“application/json”中检查我的标题..

    对不起,伙计们。花点时间帮助我解决我的问题 :)

    【讨论】:

      【解决方案3】:

      看起来你没有用 bodyParser 正确填充 req.body

      这取自express website


      req.body

      包含在请求正文中提交的数据键值对。默认情况下,它是未定义的,并在您使用 body-parser 和 multer 等正文解析中间件时填充。

      以下示例展示了如何使用正文解析中间件来填充 req.body。

      var app = require('express')();
      var bodyParser = require('body-parser');
      
      app.use(bodyParser.json()); // for parsing application/json
      app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
      
      
      replaceUser: async (req, res, next) => {
          //enforce that req.body must contain all the fields
          const { userId } = req.params;
          const  newUser  = req.body;
          // const result = await User.findByIdAndUpdate(userId, newUser, {new: true}).exec();
          // console.log(result)
          console.log(newUser)
          console.log(userId)
      
          // res.status(200).json(result);
          // console.log(userId, newUser)
      }
      

      注意:

      app.use(bodyParser.json()); // for parsing application/json
      app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
      

      【讨论】:

      • 仍然不起作用..当我尝试使用 mongoose.debug 时数据没有改变它说“..... findOne ...”但我使用了 findByIdAndUpdate 方法.. 有问题吗?当我将上面的依赖项与猫鼬函数一起使用时?
      • 它只是在 POST 和 GET 方法上工作.. PUT 和 PATCH 不起作用
      • 我猜测 PUT 和 PATCH 正在更新数据库,因此您必须在 req.body 中有一个值。您可能没有发布/填充它(通过表单)?
      • 你说得对,但我已经在 json 部分填写了它,在邮递员中:body > raw > 选择 json/application 然后在中间件中我设置 bodyparser.json() 我错了吗?
      • 尝试在邮递员中使用x-www-form-urlencoded 字段或form-data 并在那里输入值
      【解决方案4】:

      Nodejs 无法识别 req.body 参数,要使其正常工作,您需要将 body-parser 安装到您的项目中。 https://www.npmjs.com/package/body-parser

      链接中有几个例子。

      【讨论】:

      • 添加从'body-parser'导入bodyParser; app.use(bodyParser.json());
      【解决方案5】:

      你可以使用 express.json()

      const app = express();
      app.use(express.json());
      

      【讨论】:

      • 快递?不是正文解析器?
      【解决方案6】:

      您是否使用 body-parser 来读取请求正文?如果没有,请使用以下行..

      const bodyParser = require('body-parser');
      
      app.use(bodyParser.json());
      

      现在你可以通过 req.body 读取正文

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-26
        • 1970-01-01
        • 2021-05-20
        • 2017-11-14
        • 2021-07-06
        • 1970-01-01
        • 2011-05-21
        相关资源
        最近更新 更多