繁体   English   中英

猫鼬不保存所有领域

[英]Mongoose not saving all fields

我正在尝试保存一个新的文档记录,但有些字段被省略了,我一直无法弄清楚原因。

在这里我设置我的订单对象

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

当然,在我发布此问题后,我立即就知道了。 模式中的类型被列为“类型”而不是“类型”。

[这个答案是为了记录]我有同样的问题,在一个没有保存maxconnectionsroomNo的模式中,我有模式

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

改成

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

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

此外,当使用邮递员发送请求时,使用 content-type 作为JSON
否则,字段中的数据将不会被存储

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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