I'm having a hard time achieving this post product method. I don't completely understand why I don't be able to get my request body since im new to this backend thing.
here's my full code I need your help to debug why I dont get my request body to my post request method controller.
Model
const mongoose = require('mongoose');
const productSchema = new mongoose.Schema({
name: {
type: String,
},
...
});
const Product = mongoose.model('Product', productSchema);
module.exports = Product;
Controller
const Product = require('../models/productModel');
...
const createProduct = (req, res) => {
const {
name,
...
} = req.body;
console.log(name);
...
};
module.exports = {
...
createProduct,
...
};
Here in controller found out that the console.log(name) returns undefine and and I don't know why.
Routes
const express = require('express');
const router = express.Router();
const productController = require('../controllers/productController');
...
// POST /api/products
router.post('/', productController.createProduct);
...
module.exports = router;
index.js
const express = require('express');
const bodyParser = require('body-parser');
// route imports
const productRoutes = require('./routes/productRoutes');
// local imports
const coonectDB = require('./db.js')
// Set up Express app
const app = express();
app.use(express.json());
// Set up body-parser middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Set up routes
app.use('/api/product', productRoutes);
// Start the server
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
coonectDB()
.then(() => {
console.log('db connection succeeded.')
})
.catch(err => console.log(err))
Postman Request Post using postman check image
VS Code Terminal VS Code terminal logs
Thank you guys in advance and more power!