【问题标题】:Req.body gets undefined in node using expressReq.body 使用 express 在节点中未定义
【发布时间】:2020-04-14 12:44:37
【问题描述】:

我正在使用快递。
这是我尝试过的代码,在 user.controller 中,我变得未定义。

index.js

    const express = require('express');
    const mongoose = require('mongoose');
    const cors = require('cors');
    var bodyParser = require('body-parser');

    const app = express();

    //import route
    const userRoutes = require('./routes/user');

    //use express function
    app.use(cors());
    app.use(express.json());
    app.use(bodyParser.json()); // commenting this line also did not worked
    app.use(bodyParser.urlencoded({ extended: true })); // commenting this line also did not worked
    app.use(express.urlencoded({ extended: false }));

    app.use('/user' , userRoutes);

user.js(路由器)

const express = require('express');
const userController = require('../controller/user.controller');

var router = express.Router();

router.post('/signUp' , userController.signUp);

module.exports = router;

user.controller.js

const userModel = require('../models/user.model');
var userController = {};

userController.signUp = function(req, res){
    console.log("it works ======>" , req.body); // here i m getting undefined .
}

module.exports = userController;

这是我从邮递员寄来的身体

这是我的标题 .

我已经浏览了所有的 SO 链接并尝试了所有方法,但这并没有成功,因此我单独询问。


我没有得到这种奇怪的行为。

【问题讨论】:

  • 你能显示 Postmen 中设置了哪些标题吗?
  • 我没有发送标头。我已取消标记标题。 @Ejaz47
  • 您提供的邮递员图片显示标题(10)。
  • 好的,让我也展示一下我的标题。 @Ejaz47
  • 更新了我的代码。你可以结帐@Ejaz47

标签: node.js express postman


【解决方案1】:

在邮递员正文选项中,当您选择原始和 JSON 时邮递员会自动添加 Content-Type: application/json 标头。但是您似乎取消选中该标题。

Postman 不发送未选中的标头。所以你不能在 express 端访问请求体。

当您检查(选择)该标题并尝试时,它将起作用。

如果你使用 express 版本 4.16+,也不需要安装和使用body-parser 包。

【讨论】:

  • 我也尝试使用未选中的标题以及安装 body-parser 。但这不起作用。
  • @PushprajsinhChudasama 正如我在回答中所说,Content-Type: application/json 必须检查。
  • 朋友,两种方法我都试过了。通过检查和未检查。
  • @PushprajsinhChudasama 好的,必须始终检查它,并且在正文选项 raw 和 JSON 选项中也是如此。最后去掉body-parser相关代码,直接使用app.use(express.json());
【解决方案2】:

您的最后一个中间件会覆盖正确的设置。

...
    app.use(bodyParser.urlencoded({ extended: true })); // Good
    app.use(express.urlencoded({ extended: false })); // Bad, above line has been disabled.
...

如果您使用的 express 版本 > 4.x ,您可以使用 Express 中的内置中间件功能,而不是 body-parser (相同)。

您的 index.js 文件将位于:

    const express = require('express');
    const mongoose = require('mongoose');
    const cors = require('cors');
    var bodyParser = require('body-parser');

    const app = express();

    //import route
    const userRoutes = require('./routes/user');

    //use express function
    app.use(cors());
    app.use(express.json());
    // app.use(bodyParser.json()); // remove
    // app.use(bodyParser.urlencoded({ extended: true })); // remove 
    app.use(express.urlencoded({ extended: true })); // `true` instead of `false` 

    app.use('/user' , userRoutes);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2017-11-16
    • 2021-04-04
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    相关资源
    最近更新 更多