Javascript node js async/await:为什么我可以';你得不到数据?请求正文

Javascript node js async/await:为什么我可以';你得不到数据?请求正文,javascript,node.js,api,async-await,Javascript,Node.js,Api,Async Await,我正在Node.js上学习async/await来制作restful api,我在PUT和PATCH方法中遇到了一个问题,其中对于req.body它无法显示我想要的数据 代码如下:控制器/用户 replaceUser: async (req, res, next) => { //enforce that req.body must contain all the fields const { userId } = req.params; const newUse

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

代码如下:
控制器/用户

 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
功能处于活动状态,此方法适用于
GET
POST

我使用:

    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "express-promise-router": "^3.0.3",
    "mongoose": "^5.3.1",
我已经在app.js中设置了bodyparser中间件。。但对于补丁和PUT方法仍然不起作用:(


请帮帮我。我被卡住了。谢谢你。

Nodejs无法识别req.body参数,为此,你需要在项目中安装body解析器。


链接中有几个示例。

Nodejs无法识别req.body参数,要使其正常工作,您需要在项目中安装body解析器。


链接中有两个示例。

您是否使用正文解析器读取请求正文?如果没有,请使用下面的行

const bodyParser = require('body-parser');

app.use(bodyParser.json());

现在您可以按请求读取正文了。正文

是否使用正文分析器读取请求正文?如果没有,请使用下面的行

const bodyParser = require('body-parser');

app.use(bodyParser.json());
现在,您可以通过req.body来读取正文

您可以使用express.json()

您可以使用express.json()


看起来您没有正确地使用bodyParser填充req.body

这是从


要求正文

包含请求正文中提交的数据的键值对。默认情况下,它是未定义的,并且在使用正文解析中间件(如正文解析器和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

看起来您没有正确地使用bodyParser填充req.body

这是从


要求正文

包含请求正文中提交的数据的键值对。默认情况下,它是未定义的,并且在使用正文解析中间件(如正文解析器和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
[已解决] 最后,我从req.body获得了数据。问题是..我忘了在postman中的“application/json”中检查了我的头

对不起,伙计们……请慢慢来帮我解决问题:)

[SOLVED] 最后,我从req.body获得了数据。。问题是。。我忘了在邮递员的“application/json”中检查了我的标题



对不起,伙计们。。请慢慢来帮助解决我的问题:)

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

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

对不起,我误读了您的代码。。。你在用expressjs吗?你有没有包括身体分析器?是的,我已经安装了一个主体解析器。。对不起,不完整information@boogeywoogy你能记录下
req.params
req.body
的值吗?这很酷@boogeywoogy-现在这两个答案都是多余的:p可能问题出在客户端代码中,在这种情况下对不起,我误读了你的代码。。。你在用expressjs吗?你有没有包括身体分析器?是的,我已经安装了一个主体解析器。。对不起,不完整information@boogeywoogy您可以记录
req.params
req.body
的值吗?这很酷@boogeywoogy-现在这两个答案都是多余的:可能问题在于客户端代码在这种情况下,从“body parser”添加导入bodyParser;use(bodyParser.json());从“body parser”添加导入bodyParser;use(bodyParser.json());快车?不是体分析器?express?不是正文分析器?还是不起作用。。当我尝试使用mongoose.debug时,数据没有改变,它显示“…findOne…”,但我使用了findByIdAndUpdate方法。。有什么问题吗?当我将上面的依赖项与mongoose函数一起使用时?它只是在POST和GET方法上工作。。PUT和PATCH不工作我猜PUT和PATCH正在更新数据库,因此您必须在req.body中有一个值。您可能没有发布/填充它(通过表单)?是的,但我已经在json部分中填写了它,在postman:body>raw>choose json/application中,然后在我设置的中间件bodyparser.json()中我错了吗?请尝试使用postman中的
x-www-form-urlencoded
字段或
form data
,然后在其中输入值仍然无效。。当我尝试使用mongoose.debug时,数据没有改变,它显示“…findOne…”,但我使用了findByIdAndUpdate方法。。有什么问题吗?当我将上面的依赖项与mongoose函数一起使用时?它只是在POST和GET方法上工作。。PUT和PATCH不工作我猜PUT和PATCH正在更新数据库,因此您必须在req.body中有一个值。您可能没有发布/填充它(通过表单)?是的,但我已经在json部分填写了它,在postman:body>raw>选择json/application中,然后在我设置的中间件bodyparser.json()中,我错了吗?尝试在postman中使用
x-www-form-urlcoded
字段或
表单数据
,然后在那里输入值