简体   繁体   中英

Mongoose not saving all fields

I am trying to save an new document record and some of the fields are being omitted and I have not been able to figure out why.

Here I am setting my order object

var obj = new Order(orderObj);
console.log('orderObj', orderObj);
console.log('obj', obj);

output from console.log:

orderObj { customerId: '5806e2a1759fd9ad7a1f60eb',
  products: 
   [ { productId: '5806e7e3d0073cb4d2946aad',
       customerPrice: 100,
       notes: 'test',
       originalPrice: 99.95,
       priceOverride: true } ],
  notes: 'some optional order notes',
  createdBy: 'test',
  total: 99.95 }

obj { customerId: 5806e2a1759fd9ad7a1f60eb,
  notes: 'some optional order notes',
  createdBy: 'test',
  _id: 5808171e802121505e33b252,
  shipped: false,
  shippedDate: 2016-10-20T01:00:14.019Z,
  status: 'Created, Not Shipped',
  products: 
   [ { productId: 5806e7e3d0073cb4d2946aad,
       _id: 5808171e802121505e33b253,
       quantity: 1 } ] }

Here is my schema:

var orderSchema = new mongoose.Schema({
  customerId: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer'},
  products: [{
    productId: { type: mongoose.Schema.Types.ObjectId, ref: 'Product'},
    quantity: { type: Number, default: 1 },
    originalPrice: { Type: Number },
    customerPrice: { Type: Number },
    priceOverride: { Type: Boolean, default: false },
    notes: { Type: String }
  }],
  notes: String,
  status: { type: String, default: "Created, Not Shipped" },
  orderDate: { type: Date },
  shippedDate: { type: Date, default: Date.now },
  shipped: { type: Boolean, default: false },
  total: {Type: Number, default: 0.00 },
  createdBy: { type: String }
}, schemaOptions);

I cannot figure out why total and product fields (originalPrice, priceOverride) are being dropped from the mongoose Schema instance object.

Of course I figured it out right after I posted this question. The type in the Schema was listed as Type instead of type.

[this answer is for the record] I had same kind of problem and in a schema where maxconnections and roomNo were not being saved, I had schema

maxconnections: {type: Number | String },
roomNo: {type: Number | String },

changed it to

maxconnections: {type: Number },
roomNo: {type: String},

and now it works fine (.._.)

Also, when sending a request using postman, use content-type as JSON
Otherwise, data in fields will not get stored

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM