我有两个模型user-roles
和candidate-details
,我想将user-roles
集合id
作为candidateId
填充到candidate-details
中
我的用户角色模型
const userRolesSchema=mongoose.Schema({
phoneNumber:{
type:Number,
unique:false,
validate(value){
if(value.toString().length !== 10){
throw new Error('Phone number should be of 10 digits');
}
},
},
isCandidate:{
type:Boolean
},
isCompany:{
type:Boolean
}
})
我的候选人详细信息模型
const candidateRegisterationSchema=new Schema({
firstName:{
type:String,
required:true
},
lastName:{
type:String,
required:true
},
userName:{
type:String,
required:true,
unique:true
},
phoneNumber:{
type:Number,
unique:true,
required:true,
validate(value){
if(value.toString().length !== 10){
throw new Error('Phone number should be of 10 digits');
}
}
},
whatsappNumber:{
type:Number,
unique:true,
required:true,
validate(value){
if(value.toString().length !== 10){
throw new Error('Phone number should be of 10 digits');
}
},
},
candidateId:{
type:mongoose.Schema.Types.ObjectId,
ref:"user-roles",
required:true
}
})
我的候选人详细信息控制器
const candidateRegisteration = async (req, res) => {
const candidate = new CandidateRegisterationModel();
const user = await UserRolesModel.findOne({ _id: req.params.c_id }).populate({path:'candidateId',strictPopulate: false });
if (user !== null) {
try {
candidate.firstName = req.body.firstName;
candidate.lastName = req.body.lastName;
candidate.userName = req.body.userName;
// candidate.candidateId = user._id;
candidate.whatsappNumber = req.body.whatsappNumber;
candidate.phoneNumber = user.phoneNumber;
await candidate.save();
res.status(201).json({
message: "candidate saved successfully",
statusCode: res.statusCode,
});
} catch (error) {
console.log(error);
res.status(500).json({ error, statusCode: res.statusCode });
}
}
else{
res.status(404).json({
message:"candidate not found",
statusCode:res.statusCode
})
}
};
我是一个新手,我需要你的帮助来理解和修复这个填充方法请,我试图发送请求获得错误一样
name": "ValidatorError",
"message": "Path `candidateId` is required.",
这意味着它不会填充candidateId中的id
1条答案
按热度按时间p8h8hvxi1#