簡體   English   中英

自定義模式的貓鼬typeError

[英]Mongoose typeError with custom Schema

我正在嘗試使用discountSchema作為類型。 但我收到此錯誤:

throw new TypeError('Undefined type at `' + path +
      ^
TypeError: Undefined type at `discount`

但是如果我轉換為array類型:

    discount: {
        type: [discountSchema],
        default:{}
    }

有用。

我怎樣才能在貓鼬中使用復雜類型呢? 我是否以錯誤的方式使用此模型? 我如何像這樣建模這個對象?

var discountSchema = new Schema({
    type: {type: String,default:'' },
    quantity: {type: Number,default:0 },
    value: {type: Number,default:0 }
});
var objectEncomendaPropertiesSchema = {
    recheios:{
        type:[String],
        default: [],
        select: true
    },
    availableEncomenda: {
        type: Boolean,
        default:false
    },
    discount: {
        type: discountSchema,
        default:{}
    }
};

您不能存儲的embedded documents 設置為貓鼬中的單個屬性,它們總是存儲在arrays中

與該行為最接近的事情是將您的屬性設置為帶有refObjectId ,並使用populate方法來獲取它。 在這里看看這種方法是如何工作的。


查看嵌入式文檔docs

GitHub上有一個公開問題 ,要求您執行所需的行為。

您是否要創建兩個模式然后將它們連接起來?

var discountSchema = new Schema({
    type: {type: String,default:'' },
    quantity: {type: Number,default:0 },
    value: {type: Number,default:0 }
});

mongoose.model('Discount', discountSchema);

var objectEncomendaPropertiesSchema = new Schema({
    recheios:{
        type: String,
        default: '',
        select: true
    },
    availableEncomenda: {
        type: Boolean,
        default:false
    },
    discount: {
        type: Schema.Types.ObjectId,
        ref: 'Discount'
    }
})

mongoose.model('objectEncomendaProperties', objectEncomendaPropertiesSchema); 

我引用第二個模式中的折扣以通過ObjectId引用第一個模式
它將獲取Discount模型中的屬性作為第二個Schema中的Discount屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM