【问题标题】:Mongoose not saving all fields猫鼬不保存所有字段
【发布时间】:2021-03-05 21:25:13
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: javascript node.js mongoose


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      改成

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

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

      【讨论】:

        猜你喜欢
        • 2019-04-13
        • 1970-01-01
        • 1970-01-01
        • 2017-09-14
        • 2016-05-05
        • 1970-01-01
        • 1970-01-01
        • 2017-12-05
        • 2015-11-16
        相关资源
        最近更新 更多