title: Mongoose Schema type Object Id Mapping
body: <p>Backend Code:
  This is orderModel.js</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import mongoose from "mongoose";
  
  const orderSchema = mongoose.Schema(
    {
      user: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: "User",
      },
      orderItems: [
        {
          name: { type: String, required: true },
          qty: { type: String, required: true },
          image: { type: String, required: true },
          price: { type: String, required: true },
          productId: {
            type: mongoose.Schema.Types.ObjectId,
            required: true,
            ref: "Product",
          },
        },
      ],
      deliveryAddress: {
        address: { type: String, required: true },
        area: { type: String, required: true },
        // gender: { type: String, required: true },
        postalCode: { type: String, required: true },
        district: { type: String, required: true },
        division: { type: String, required: true },
        country: { type: String, required: true, default: "Bangladesh" },
      },
      paymentMethod: {
        type: String,
        required: true,
      },
      paymentStatus: {
        id: { type: String },
        status: { type: String },
        update_time: { type: String },
        email: { type: String },
      },
      taxPrice: {
        type: Number,
        required: true,
        default: 0.0,
      },
      deliveryCost: {
        type: Number,
        required: true,
        default: 0.0,
      },
      totalPrice: {
        type: Number,
        required: true,
        default: 0.0,
      },
      discount: {
        type: Number,
        required: true,
        default: 0.0,
      },
      isPaid: {
        type: Boolean,
        required: true,
        default: false,
      },
      paidAt: {
        type: Date,
      },
      isDelivered: {
        type: Boolean,
        required: true,
        default: false,
      },
      deliveredAt: {
        type: Date,
      },
    },
    {
      timeStamps: true,
    }
  );
  
  const Order = mongoose.model("Order", orderSchema);
  
  export default Order;</code></pre>
  </div>
  </div>
  </p>
  <p>This is orderController.js -</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import asyncHandler from "express-async-handler";
  import Order from "../models/orderModel.js";
  
  // review: desc =&gt; Create New Order
  // review: route =&gt; POST api/orders
  // review: access =&gt; Private
  
  const createOrder = asyncHandler(async (req, res) =&gt; {
    const {
      orderItems,
      deliveryAddress,
      paymentMethod,
      taxPrice,
      deliveryCost,
      discount,
      totalPrice,
    } = req.body;
  
    if (orderItems &amp;&amp; orderItems.length === 0) {
      res.status(400);
      throw new Error("No order items");
    } else {
      try {
        
        const order = new Order({
          orderItems,
          user: req.user._id,
          deliveryAddress,
          paymentMethod,
          taxPrice,
          deliveryCost,
          discount,
          totalPrice,
        });
        console.log("ORDER CREATED", order);
        const createdOrder = await order.save();
  
        console.log("ORDER saved", createdOrder);
  
        res.status(201).json(createdOrder);
      } catch (error) {
        res.status(400);
        throw new Error("Invalid order data", error);
      }
    }
  });
  
  // review: desc =&gt; GET order by id
  // review: route =&gt; GET api/orders/:id
  // review: access =&gt; private
  
  const getOrderById = asyncHandler(async (req, res, next) =&gt; {
    const order = await Order.findById(req.params.id).populate(
      "user",
      "name email"
    );
  
    if (order) {
      res.json(order);
    } else {
      res.status(404);
      throw new Error("Order not found");
    }
  });
  
  export { createOrder, getOrderById };</code></pre>
  </div>
  </div>
  </p>
  <p>Frontend Code:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>    const handleSubmit = (e) =&gt; {
          e.preventDefault();
          dispatch(
            createOrder({
              orderItems: cartItems,
              deliveryAddress: addressObject,
              paymentMethod,
              taxPrice: vat,
              deliveryCost,
              discount,
              totalPrice,
            })
          );
        };</code></pre>
  </div>
  </div>
  </p>
  <p>Here When I am sending request to backend, It's giving me validation error. As - orderItems.0.productId: Path <code>productId</code> is required. Why I am getting this error ? mongoose should not map data and get productId from sent cartItems, where is a field is _id ? Your time and  help is most appreciated, Thanks.</p>