Home > Blockchain >  How to create an "order" object at the same time as user
How to create an "order" object at the same time as user

Time:09-27

I'm working on an application to order food without forcing the user to log in. He can order by providing information just during the order. when I try to create a command I want to create my user at the same time. My issue is that when creating the order an error message is displayed and I don't know what to do to solve this problem. Model - Order

const mongoose = require('mongoose');
const User = require('./User')
const Meal = require('./Meal')
const OrderSchema = new mongoose.Schema(
  {
    customer: {
      type: mongoose.Schema.Types.ObjectId,
      ref: User,
      required: true
    },
    orderedmeal: {
      type: mongoose.Schema.Types.ObjectId,
      ref: Meal,
      required: true
    },
    ordertotal: {
      type: Number,
      required: false,
      default:0
    },
    dateOrder: String,
    status: {
      type: Boolean,
      default: false
    },
  },
  { timestamps: true }
);

module.exports = mongoose.model("Order", OrderSchema);

Controller - create Order

exports.createOrder = async (req, res) => {

  const newOrder = new Order(req.body)
  newOrder.save();

  return res.status(200).json({
    customer: newOrder.customer, 
    orderedmeal: newOrder.orderedmeal,
    ordertotal: newOrder.ordertotal,
    dateOrder: newOrder.dateOrder,
    status : newOrder.status
  })
}

Orderform

const [order, setOrder] = useState();
  
  const handleSubmit = async (e) => {
      e.preventDefault();
      try {
        const res = await axios.post(GET_ORDERS_URL, {
          customer: {
            firstname: firstname,
            lastname: lastname,
            numTel: numTel,
            address: address,
          },
          orderedmeal: orderedmeal,
          ordertotal: orderTotal,
          dateOrder: dateOrder,
        });

        setOrder(res.data);
        console.log("Order ", order);
      } catch (error) {
        console.log(error);
      }
  };

Résults on Console

    this.$__.validationError = new ValidationError(this);
                                ^

 ValidationError: Order validation failed: customer: Cast to ObjectId failed for value "{
   firstname: 'djamila',
   lastname: 'azouane',
   numTel: '0777776602',
   address: 'Hussein Dey'
 }" (type Object) at path "customer" because of "BSONTypeError"
     at model.Document.invalidate (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:3054:32)
     at model.$set (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:1409:12)
     at model.$set (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:1092:16)
     at model.Document (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:166:12)
     at model.Model (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\model.js:119:12)
     at new model (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\model.js:4999:15)
     at exports.createOrder (C:\Users\hp\Documents\booking\server\Controllers\order.js:9:20)
     at Layer.handle [as handle_request] (C:\Users\hp\Documents\booking\server\node_modules\express\lib\router\layer.js:95:5)
     at next (C:\Users\hp\Documents\booking\server\node_modules\express\lib\router\route.js:144:13)
     at next (C:\Users\hp\Documents\booking\server\node_modules\express\lib\router\route.js:138:14) {
   errors: {
     customer: CastError: Cast to ObjectId failed for value "{
       firstname: 'djamila',
       lastname: 'azouane',
       numTel: '0777776602',
       address: 'Hussein Dey'
     }" (type Object) at path "customer" because of "BSONTypeError"
         at ObjectId.cast (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\schema\objectid.js:248:11)
         at ObjectId.SchemaType.applySetters (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\schematype.js:1201:12)    
         at model.$set (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:1367:22)
         at model.$set (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:1092:16)
         at model.Document (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:166:12)
         at model.Model (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\model.js:119:12)
         at new model (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\model.js:4999:15)
         at exports.createOrder (C:\Users\hp\Documents\booking\server\Controllers\order.js:9:20)
         at Layer.handle [as handle_request] (C:\Users\hp\Documents\booking\server\node_modules\express\lib\router\layer.js:95:5)      
         at next (C:\Users\hp\Documents\booking\server\node_modules\express\lib\router\route.js:144:13) {
       stringValue: '"{\n'  
         "  firstname: 'djamila',\n"  
         "  lastname: 'azouane',\n"  
         "  numTel: '0777776602',\n"  
         "  address: 'Hussein Dey'\n"  
         '}"',
       messageFormat: undefined,
       kind: 'ObjectId',
       value: {
         firstname: 'djamila',
         lastname: 'azouane',
         numTel: '0777776602',
         address: 'Hussein Dey'
       },
       path: 'customer',
       reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
           at new BSONTypeError (C:\Users\hp\Documents\booking\server\node_modules\bson\lib\error.js:41:28)
           at new ObjectId (C:\Users\hp\Documents\booking\server\node_modules\bson\lib\objectid.js:67:23)
           at castObjectId (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\cast\objectid.js:25:12)
           at ObjectId.cast (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\schema\objectid.js:246:12)
           at ObjectId.SchemaType.applySetters (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\schematype.js:1201:12)  
           at model.$set (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:1367:22)
           at model.$set (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:1092:16)
           at model.Document (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:166:12)
           at model.Model (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\model.js:119:12)
           at new model (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\model.js:4999:15),
       valueType: 'Object'
     }
   },
   _message: 'Order validation failed'
 }
 [nodemon] app crashed - waiting for file changes before starting...

Frontend Navigation Console Frontend   Console navigation

Thank you,

CodePudding user response:

In your order schema, customer has type "ObjectId", and when you pass req to create order, you pass customer object, which is causing the error. In your createOrder function, first, you need to save the customer object and then use its id to create an order.

  • Related