Mongoose未保存所有字段

vsnjm48y  于 2022-12-19  发布在  Go
关注(0)|答案(3)|浏览(175)

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

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示例对象中删除total和product字段(originalPrice、priceOverride)。

dohp0rv5

dohp0rv51#

当然,我在发布这个问题后就想通了。架构中的类型被列为Type而不是Type。

8ljdwjyq

8ljdwjyq2#

[this答案仅供参考]我遇到过同样的问题,在未保存maxconnectionsroomNo的模式中,我遇到了模式

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

改为

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

现在它工作得很好(!._.)

o7jaxewo

o7jaxewo3#

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

相关问题