对如何获得req.body感到困惑 [英] Confused about how to get req.body

查看:60
本文介绍了对如何获得req.body感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法在服务器上显示身体数据.我实际上是想在post/put/fetch调用中获取此信息,但是为了解决该问题,我将其简化为一个简单的.get,但它仍然不会出现.谁能看到为什么服务器上没有显示正文?因此,我无法以更复杂的方式完成任何工作(例如获取请求的正文,但现在仍然坚持这个简单的示例.)

此代码可以正常工作,并且可以发送数据,只是似乎无法访问服务器上的正文.

server.js

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

app.use(bodyParser.json());
const port = process.env.PORT || 8000;

const Cat = require('./Cat');
const Dog = require('./Dog');

app.route('/animals')
  .get(function (req, res)  {
    console.log(req.body, 'req.body log'); //this returns {}
    res.send({ Cat, Dog });
  })

app.listen(port, () => console.log(`Listening on port ${port}`));

在反应中,如果我调用以下callApi()函数,console.log会在前端显示主体数据,并且可以在页面上使用该数据.

客户电话

callApi = async () => {
  const response = await fetch('/animals');
  const body = await response.json();
  console.log(body) //shows all the data just fine!

  if (response.status !== 200) throw Error(body.message);

  return body;
};

使用节点9并表示4.

解决方案

我认为您在混淆请求和响应对象.但是除此之外,我将解释在哪里/如何从GET和POST/PUT请求中传递数据.

发出GET请求后,您可以通过查询参数(即/animals?type = cat)将数据传递到服务器.这些参数将已经在名为req.query的对象中解析.

发出POST或PUT请求后,您已经应用了正文解析中间件(已完成),则JSON将作为req.body下的已解析对象提供.

在您的示例中,您已发出GET请求,但未提供任何查询字符串参数.因此req.body将返回一个空对象,req.query也将返回.

您的客户呼叫显示了数据,因为您已经通过res.send()将数据发送回了响应中.这完全与为什么req.body在您的情况下为空对象无关.

尝试在客户呼叫中使用fetch('/animals?type=cat').然后,您会看到req.query返回{ type: 'cat' }.

I cannot seem to get the body data shown on my server. I am actually trying to get this in post/put/fetch calls, but to try to fix the problem, i've boiled it down to a simple .get, and it still won't appear. Can anyone see why the body isn't showing on the server? I'm unable to get anything done in more complicated called due to this (like get the body of the req, but sticking to this simple example for now.)

This code is a fully working and sends data, just cant seem to access the body on the server.

server.js

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

app.use(bodyParser.json());
const port = process.env.PORT || 8000;

const Cat = require('./Cat');
const Dog = require('./Dog');

app.route('/animals')
  .get(function (req, res)  {
    console.log(req.body, 'req.body log'); //this returns {}
    res.send({ Cat, Dog });
  })

app.listen(port, () => console.log(`Listening on port ${port}`));

In react, if I call the following callApi() function, console.log shows the body data just fine on the front end, and the data can be used on the page.

client call

callApi = async () => {
  const response = await fetch('/animals');
  const body = await response.json();
  console.log(body) //shows all the data just fine!

  if (response.status !== 200) throw Error(body.message);

  return body;
};

Using node 9 and express 4.

解决方案

I think you're confusing the request and response objects. But aside from that, I'll explain where/how to get data passed in from GET and POST/PUT requests.

When a GET request is made, you can pass data to the server via query params (i.e. /animals?type=cat). These parameters will be available already parsed in an object called req.query.

When a POST or PUT request is made, and you've applied the body parsing middleware (which you have done), the JSON will be available as a parsed object under req.body.

In your example, you have made a GET request, and have no provided any query string parameters. So req.body will return an empty object, as will req.query.

Your client call shows data because you've sent data back in the response via res.send(). This is totally unrelated to why req.body is an empty object in your case.

Try using fetch('/animals?type=cat') in your client call. Then, you will see that req.query returns { type: 'cat' }.

这篇关于对如何获得req.body感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆