0

I try to test post request in thunder vscode to create new order but it gives me this error in screenshot.

test post request with thunder vscode

I send post request in format json but the error is not resolved; when I remove required from ordermodel it works.

This is my node version: Node.js v18.12.1

This is the error (backticks removed for ease of reading):

{ "msg": "Order validation failed: shippingInfo.phoneNum: Path shippingInfo.phoneNum is required., shippingInfo.pincode: Path shippingInfo.pincode is required., shippingInfo.country: Path shippingInfo.country is required., shippingInfo.state: Path shippingInfo.state is required., shippingInfo.city: Path shippingInfo.city is required., shippingInfo.address: Path shippingInfo.address is required." }

This is the request body:

{
  "shippingInfo": {
    " address": "tunisia",
    "city": "tunisia",
    "state": "tunisia",
    "country": "tunisia",
    "pincode": 7100,
    "phoneNum": 11558899
  }
}
const mongoose = require("mongoose");
const { Schema } = mongoose;
const orderSchema = new mongoose.Schema({
    shippingInfo: {
        address: {type: String,required: true},
        city: {
            type: String,
            required: true
        },
        state: {
            type: String,
            required: true
        },
        country: {
            type: String,
            required: true
        },
        pincode: {
            type: Number,
            required: true
        },
        phoneNum: {
            type: Number,
            required: true
        },
    },
    
});
const orderModel = mongoose.model("Order", orderSchema);

module.exports=orderModel
const orderModel = require("../models/orderModel");

//Create new order
;
const newOrder = async (req, res) => {
    const {shippingInfo}=req.body
    try {
     const order= await new orderModel({shippingInfo})
    // console.log(shippingInfo)
     console.log(order)
    await order.save()
    res.status(201).send({ order: order, msg: "order added successfully" })

    } catch (error) {
      // console.log(error);
      res.status(400).send({ msg: error.message });
    }
  };

  module.exports = {newOrder };
const express = require("express");
const { newOrder } = require("../controllers/orderController");
const isAuth = require("../middlewares/isAuth");
const router = express.Router();


/**
 * @Params POST /order/neworder
 * @description create new order
 * @acces protected
 */
router.post("/neworder",isAuth(),newOrder);


module.exports = router; 
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Is there a way in this framework to log the object it believes you have sent? Perhaps your request object is not getting to Mongoose. – halfer Jan 02 '23 at 12:02

1 Answers1

0

There's no issue in the code, everything looks good. The problem is in the payload, that you're sending.

{
  "shippingInfo": {
    " address": "tunisia", // address has white space in the key, remove it
    "city": "tunisia",
    "state": "tunisia",
    "country": "tunisia",
    "pincode": 7100,
    "phoneNum": 11558899
  }
}

This whitespace is causing validation errors. Once you remove it, there wont be any error

Rizwan
  • 363
  • 3
  • 10