Node.js 如何在mongoose中使用嵌套模式,使用';类型';,创建数组?
我正在尝试创建一个嵌套mongoose模式,该模式使用“type”创建嵌套数组 我认为我有问题的模式是“chorePerson” 以下是我试图将其放入模式中的数据:Node.js 如何在mongoose中使用嵌套模式,使用';类型';,创建数组?,node.js,mongoose,nested,schema,Node.js,Mongoose,Nested,Schema,我正在尝试创建一个嵌套mongoose模式,该模式使用“type”创建嵌套数组 我认为我有问题的模式是“chorePerson” 以下是我试图将其放入模式中的数据: { "chart": [ { "ordinal": 0, "chorePerson": [ { "person": "emily", "chore": "Catbox" }, { "pers
{
"chart": [
{
"ordinal": 0,
"chorePerson": [
{
"person": "emily",
"chore": "Catbox"
},
{
"person": "Steve",
"chore": "Dishes"
}
]
}
]
这是我当前的模式。注意“类型”用于“图表”和“舞蹈人”
当我运行代码时,这是崩溃前得到的结果:
{ _id: 5c742ed116a095522c38ddfc,
affiliation: 'family',
currentYear: 2019,
currentWeekNumber: 9,
date: 2019-02-25T20:26:33.914Z,
chart: [ { ordinal: 0, chorePerson: [Array] } ] }
我想。。。chorePerson正在导致错误。。。但我不知道如何修复它。
例外情况如下:
(node:6728) UnhandledPromiseRejectionWarning: ObjectExpectedError: Tried to set nested object field `chart` to primitive value `[object Object]` and strict mode is set to throw.
我尝试过的
我尝试了这个模式:
const chartSchema = new mongoose.Schema({
ordinal: {type: Number, required: true},
chorePerson : [{
person : String,
chore : String
}]
});
更新:
好的。。。所以我回到了基础,这是可行的,但这不是我想要的最终模式。有人能帮忙筑巢吗
// create the schema
const ChoreChartSchema = new Schema({
affiliation: {type: String, required: true},
currentWeekNumber: {type: Number, required: true},
currentYear: {type: Number, required: true},
// chart:{ type: chartSchema },
chart:[{
ordinal: 0,
chorePerson : [{
person : String,
chore : String
}]
}],
date: {type: Date, default: Date.now},
})
结果比我想象的要容易:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const chorePersonSchema = new mongoose.Schema({
person: {type: String, requried: true},
personID: {type: String, required: true},
chore: {type: String, required: true},
choreID: {type: String, required: true},
});
const chartSchema = new mongoose.Schema({
ordinal: {type: Number, required: true},
chorePerson : [{ type:chorePersonSchema }]
});
// create the schema
const ChoreChartSchema = new Schema({
affiliation: {type: String, required: true},
weekNumber: {type: Number, required: true},
year: {type: Number, required: true},
chart:[{type: chartSchema}],
date: {type: Date, default: Date.now},
})
module.exports = ChoreChart = mongoose.model('cm_chorechart', ChoreChartSchema)
如何编写图表:[{type:chartSchema}或{type:someOtherSchema}],我的意思是图表可以是chartSchema对象或someOtherSchema对象的数组..是否可以执行联合类型?谢谢
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const chorePersonSchema = new mongoose.Schema({
person: {type: String, requried: true},
personID: {type: String, required: true},
chore: {type: String, required: true},
choreID: {type: String, required: true},
});
const chartSchema = new mongoose.Schema({
ordinal: {type: Number, required: true},
chorePerson : [{ type:chorePersonSchema }]
});
// create the schema
const ChoreChartSchema = new Schema({
affiliation: {type: String, required: true},
weekNumber: {type: Number, required: true},
year: {type: Number, required: true},
chart:[{type: chartSchema}],
date: {type: Date, default: Date.now},
})
module.exports = ChoreChart = mongoose.model('cm_chorechart', ChoreChartSchema)