req.body 在 post 请求中未定义
req.body is undefined in a post request
我正在尝试将 post 函数内请求正文的内容打印到控制台,如下所示:
export const createOffering = async (req, res) => {
const offering = req.body;
const newOffering = new Offering(offering);
console.log(req.body);
try{
//await offering.save();
res.status(201).json(newOffering);
}
catch(error){
res.status(409).json({message: error.message})
}
}
然而,控制台打印“undefined”
这是我的 index.js
的代码
import express from "express";
import mongoose from "mongoose";
import cors from "cors";
import bodyParser from "body-parser";
import offeringsRoutes from './routes/offerings.js'
const app = express();
app.use('/offerings', offeringsRoutes);
app.use(cors());
app.use(express.urlencoded({ limit: '25mb', extended: true }));
app.use(express.json({ limit: '25mb' }));
app.use
通常用于您希望在路由之前 运行 的中间件。我还看到您导入了 bodyParser
但没有使用它,虽然这不是必需的,但您可能希望通过以下方式进行重构:
import express from "express";
import mongoose from "mongoose";
import cors from "cors";
import bodyParser from "body-parser";
import offeringsRoutes from './routes/offerings.js'
const app = express();
// You generally want this middleware above your routes if you want
// them to apply to all routes that come after these.
app.use(cors());
app.use(express.urlencoded({ limit: '25mb', extended: true }));
app.use(express.json({ limit: '25mb' }));
app.use(bodyParser.json());
// If you only want to accept `POST` requests
app.post('/offerings', offeringsRoutes);
// or you can use this if you want this function to handle all requests to this route
// app.all('/offerings', offeringsRoutes);
我正在尝试将 post 函数内请求正文的内容打印到控制台,如下所示:
export const createOffering = async (req, res) => {
const offering = req.body;
const newOffering = new Offering(offering);
console.log(req.body);
try{
//await offering.save();
res.status(201).json(newOffering);
}
catch(error){
res.status(409).json({message: error.message})
}
}
然而,控制台打印“undefined”
这是我的 index.js
的代码import express from "express";
import mongoose from "mongoose";
import cors from "cors";
import bodyParser from "body-parser";
import offeringsRoutes from './routes/offerings.js'
const app = express();
app.use('/offerings', offeringsRoutes);
app.use(cors());
app.use(express.urlencoded({ limit: '25mb', extended: true }));
app.use(express.json({ limit: '25mb' }));
app.use
通常用于您希望在路由之前 运行 的中间件。我还看到您导入了 bodyParser
但没有使用它,虽然这不是必需的,但您可能希望通过以下方式进行重构:
import express from "express";
import mongoose from "mongoose";
import cors from "cors";
import bodyParser from "body-parser";
import offeringsRoutes from './routes/offerings.js'
const app = express();
// You generally want this middleware above your routes if you want
// them to apply to all routes that come after these.
app.use(cors());
app.use(express.urlencoded({ limit: '25mb', extended: true }));
app.use(express.json({ limit: '25mb' }));
app.use(bodyParser.json());
// If you only want to accept `POST` requests
app.post('/offerings', offeringsRoutes);
// or you can use this if you want this function to handle all requests to this route
// app.all('/offerings', offeringsRoutes);