req.body is undefined in a post request

I'm trying to print to the console the content of the request's body inside a post function as follows:

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})
}

}

However, the console prints "undefined"

Here is the code for my 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' }));


Read more here: https://stackoverflow.com/questions/68461036/req-body-is-undefined-in-a-post-request

Content Attribution

This content was originally published by PINK at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: