嘿,所以我试图用 trainee.name 填充会议,这不起作用,并到达 try catch 块中的 catch 并返回 500。(使用 fastify+mongoose)
模型/会议.ts
import uniqueValidator from 'mongoose-unique-validator';
import { Schema, Types, model } from 'mongoose';
interface MeetingType {
_id: Types.ObjectId;
coach: Types.ObjectId;
trainees: Types.ObjectId[];
}
const meetingSchema = new Schema({
coach: { type: Types.ObjectId, required: true, ref: 'Coach' },
trainees: [{ type: Types.ObjectId, required: true, ref: 'Trainee' }],
not relevant info about meeting..
});
meetingSchema.plugin(uniqueValidator);
const Meeting = model('meeting', meetingSchema);
export default Meeting;
export type { MeetingType };
模型/trainee.ts
import uniqueValidator from 'mongoose-unique-validator';
import { Schema, Types, model } from 'mongoose';
interface TraineeType {
trainee's interface...
}
const traineeSchema = new Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true, match: /@/ },
meetings: [{ type: Types.ObjectId, required: true, ref: 'Meeting' }],
activeCoachDetails: {
activeCoach: { type: Types.ObjectId, ref: 'Coach' },
startDateCoach: { type: Date },
},
not relevant info about trainee..
});
traineeSchema.plugin(uniqueValidator);
const Trainee = model('trainee', traineeSchema);
export default Trainee;
export type { TraineeType };
我有待处理的会议功能,可以正确发送会议,但是当我尝试添加填充时,它会发送 500 并崩溃以捕获块。无需填充即可工作。我想填充返回 trainee.name
控制器.ts
const getPendingApprovalMeetings = async (
request: any,
reply: FastifyReply,
) => {
const { coachId }: { coachId: string } = request.user;
const { page, limit }: { page: number; limit: number } = request.query;
let coach: CoachType | null;
try {
coach = await Coach.findById(coachId);
} catch (err: Error | any) {
logger.error(`can't get coach by his id: ${err.message} - ${coachId}`);
return await reply
.status(500)
.send('Getting meetings failed, please try again.');
}
if (!coach) {
logger.error(`didn't find coach by his id - ${coachId}`);
return await reply
.status(404)
.send('Could not find coach for provided id.');
}
let pendingApprovalMeetings = [];
try {
pendingApprovalMeetings = await Meeting.find(
{
_id: { $in: coach.meetings },
status: 'Waiting for approval',
},
{},
{
skip: (page - 1) * limit,
limit,
},
)
.populate({
path: 'trainee',
select: 'name',
})
} catch (err: Error | any) {
logger.error(`can't get pending approval meetings - ${coachId}`, err);
return await reply
.status(500)
.send('Getting pending approval meetings failed, please try again.');
}
return await reply.status(200).send({
meetings: pendingApprovalMeetings,
});
};
首先,您可以看到以下调用记录的错误。
logger.error(`can't get pending approval meetings - ${coachId}`, err);
其次,有一个错别字。
模型会议有一个路径“受训者”,在填充时它被称为“受训者”。漏掉了“s”这个字符。
.populate({
path: 'trainee',
select: 'name',
})
请更正为
.populate({
path: 'trainees',
select: 'name',
})
最后,在检查记录的错误时,您可能会看到类似于以下内容的错误消息:
StrictPopulateError: Cannot populate path `xxxx` because it is not in your schema. Set the `strictPopulate` option to false to override.
at getModelsMapForPopulate ....