猫鼬没有保存所有字段

时间:2016-10-20 01:12:12

标签: javascript node.js mongoose

我正在尝试保存新的文档记录,并且某些字段被忽略,我无法找出原因。

我在这里设置订单对象

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

从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 } ] }

这是我的架构:

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);

我无法弄清楚为什么要从mongoose Schema实例对象中删除总数和产品字段(originalPrice,priceOverride)。

2 个答案:

答案 0 :(得分:0)

当然我在发布这个问题后就知道了。 Schema中的类型列为Type而不是type。

答案 1 :(得分:0)

[此答案仅供记录] 我遇到了同样的问题,并且在 maxconnectionsroomNo 没有被保存的架构中, 我有架构

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

改成

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

现在它工作正常 (!._.)