给出以下我正在尝试的文件,以填充数组中的所有子文件。 我是新来的。猫鼬,所以也许我在创建文档时出错, 正确填充了executionTypeModel和roundModel,但未填充子文档exerciseType。
const workout = mongoose.Schema({
name: {
type: String,
required: 'Please enter the name of the workout',
unique: true
},
workoutTypeId: { type: mongoose.Schema.Types.ObjectId, ref: 'WorkoutType' },
rounds: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Round' }]
}, { timestamps: true });
const workoutTypeModel = mongoose.Schema({ name: { type: String } }, { timestamps: true });
const roundModel = mongoose.Schema({
repets: { type: Number, max: 1000 },
exercises: { type: mongoose.Schema.Types.Array, ref: 'Exercise' }
}, { timestamps: true });
const exerciseModel = mongoose.Schema({
repets: { type: Number, max: 1000 },
exerciseType: { type: mongoose.Schema.Types.ObjectId, ref: 'ExerciseType' }
},
{ timestamps: true });
const exerciseTypeModel = mongoose.Schema({
name: {
type: String,
required: '{PATH} is required!'
},
}, { timestamps: true });
这是控制器代码:
async index(req, res) {
const workouts = await Workout.find()
.populate({ path: 'workoutTypeId' })
.populate({ path: 'rounds', populate: 'exercises exerciseType' })
res.send(workouts);
}
预先感谢