javascriptmysqlnode.jspostman

TypeError: Cannot destructure property 'userId' of 'req.body' as it is undefined


Every Time I try to do a post request in postman to http://localhost:3000/api/orders/new

I am getting this error: **TypeError: Cannot destructure property 'userId' of 'req.body' as it is undefined. at C:\Users\Web-Developer\Desktop\shoppy\backend\routes\orders.js:70:10 at Layer.handle [as handle_request] (C:\Users\Web-Developer\Desktop\shoppy\backend\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\Web-Developer\Desktop\shoppy\backend\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\Web-Developer\Desktop\shoppy\backend\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\Web-Developer\Desktop\shoppy\backend\node_modules\express\lib\router\layer.js:95:5) at C:\Users\Web-Developer\Desktop\shoppy\backend\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\Web-Developer\Desktop\shoppy\backend\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\Web-Developer\Desktop\shoppy\backend\node_modules\express\lib\router\index.js:275:10) at Function.handle (C:\Users\Web-Developer\Desktop\shoppy\backend\node_modules\express\lib\router\index.js:174:3) at router (C:\Users\Web-Developer\Desktop\shoppy\backend\node_modules\express\lib\router\index.js:47:12) **

const router = express.Router();
const {database} = require('../config/helpers');

/* GET ALL ORDERS */
router.get('/', (req, res) => {
    database.table('orders_details as od')
        .join([
            {
                table: 'orders as o',
                on: 'o.id = od.order_id'
            },
            {
                table: 'products as p',
                on: 'p.id = od.product_id'
            },
            {
                table: 'users as u',
                on: 'u.id = o.user_id'
            }
        ])
        .withFields(['o.id', 'p.title as name', 'p.description', 'p.price', 'u.username'])
        .sort({id: 1})
        .getAll()
        .then(orders => {
            if(orders.length > 0) {
                res.status(200).json(orders);
            } else {
                res.json({message: 'Mo Orders Found'})
            }
        }).catch(err => console.log(err));
})


/* GET SINGLE ORDER */
router.get('/:id', (req, res) => {

    const orderId = req.params.id;

    database.table('orders_details as od')
        .join([
            {
                table: 'orders as o',
                on: 'o.id = od.order_id'
            },
            {
                table: 'products as p',
                on: 'p.id = od.product_id'
            },
            {
                table: 'users as u',
                on: 'u.id = o.user_id'
            }
        ])
        .withFields(['o.id', 'p.title as name', 'p.description', 'p.price', 'u.username'])
        .filter({'o.id': orderId})
        .getAll()
        .then(orders => {
            if(orders.length > 0) {
                res.status(200).json(orders);
            } else {
                res.json({message: `No Orders Found with orderId ${orderId}`})
            }
        }).catch(err => console.log(err));
})

/* PLACE A NEW ORDER */
router.post('/new', (req, res) => {

    let {userId, products} = req.body;
    console.log(userId, products)
})

module.exports = router

Solution

  • app.use(bodyParser.json()) // for parsing application/json
    app.use(bodyParser.urlencoded({ extended: true }))
    

    I figured out that this code needs to be used before specifying route path.