gpt4 book ai didi

node.js - Express - `req.body` 始终为空

转载 作者:太空宇宙 更新时间:2023-11-03 22:57:34 26 4
gpt4 key购买 nike

我正在尝试构建一个完整的堆栈(MERN)应用程序。当我使用 JSON 向 Express 服务器发送请求时,我总是看到我的 req.body 为空。我在请求中无法看到我的 JSON。

我尝试仅使用 app.use(bodyParser.json())app.use(bodyParser.urlencoded({ Extended: true }));,但没有变化。

index.js

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); // for encoded bodies
console.log("App");
require("./schema/data");
require("./routes/home_routes")(app);
require("./routes/table_routes")(app);
require("./routes/utility_routes")(app);

utility_routes.js

const mongoose = require("mongoose");
const Data = mongoose.model("data");
module.exports = app => {
app.get("/api/search_member", (req, res) => {
console.log("Utility", req);
console.log("Req Done");
// const search = await Data.findById({ member_id: req.body.member_id });
// console.log(search);
});
};

请求正文

[0]   body: {},
[0] route:
[0] Route {
[0] path: '/api/search_member',
[0] stack: [ [Layer] ],
[0] methods: { get: true } } }

客户要求

 onSearch = async value => {
console.log(value);
if (value) {
const searchData = await axios.get("/api/search_member", { value });
console.log(searchData);
}
};

最佳答案

You are making a GET request. GET request do not send body. To get req.body, make POST request to server.

Firstly:-

const searchData = await axios.post("/api/search_member", { value });

And Secondly,

app.POST("/api/search_member", async (req, res) => {
console.log("Utility", req);
console.log("Req Done");
});

关于node.js - Express - `req.body` 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56624471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com