簡體   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