gpt4 book ai didi

node.js - 如何在 Mongoose Schema 中要求嵌套的 json 对象

转载 作者:行者123 更新时间:2023-12-03 12:13:34 24 4
gpt4 key购买 nike

我有以下 Mongoose 模式:

const productSchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 1,
maxlength: 255
},
extraData: {
brand: {
type: String,
required: true,
minlength: 1,
maxlength: 255
},
quantity: {
type: Number,
required: true,
minlength: 1,
maxlength: 10
},
required: true
}
});

但是,当我执行它时,我收到以下错误:“TypeError: Invalid schema configuration: True is not a valid type at path extraData.required ”。我如何需要额外数据?

最佳答案

更新:
您可以使用 Subdocument方法

const extraDataSchema = new mongoose.Schema({
brand: {
type: String,
required: true,
minlength: 1,
maxlength: 255
},
quantity: {
type: Number,
required: true,
minlength: 1,
maxlength: 10
}
});

const productSchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 1,
maxlength: 255
},
extraData: {
type: extraDataSchema, required: true
}
});

关于node.js - 如何在 Mongoose Schema 中要求嵌套的 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54873860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com