我正在尝试找到此验证错误的根本原因:(node:1572) UnhandledPromiseRejectionWarning: ValidationError: profile validation failed: education.0.fieldsofstudy: Path fieldsofstudy is required.
即使我将"研究领域"更改为其他名称,例如"专业",也会发生这种情况。仍会返回相同的错误消息。
这是我的教育个人资料代码
// @route POST api/profile/education
// @desc Add education to profile
// @access Private
router.post(
"/education",
passport.authenticate("jwt", { session: false }),
(req, res) => {
const { errors, isValid } = validateEducationInput(req.body);
// Check Validation
if (!isValid) {
// Return any errors with 400 status
return res.status(400).json(errors);
}
Profile.findOne({ user: req.user.id }).then(profile => {
const newEdu = {
school: req.body.school,
degree: req.body.degree,
fieldofstudy: req.body.fieldofstudy,
from: req.body.from,
to: req.body.to,
current: req.body.current,
description: req.body.description
};
// Add to exp array
profile.education.unshift(newEdu);
profile.save().then(profile => res.json(profile));
});
}
);
这是我的教育选项卡的代码
data.school = !isEmpty(data.school) ? data.school : "";
data.degree = !isEmpty(data.degree) ? data.degree : "";
data.fieldofstudy = !isEmpty(data.fieldofstudy) ? data.fieldofstudy:"";
data.from = !isEmpty(data.from) ? data.from : "";
if (Validator.isEmpty(data.school)) {
errors.school = "School field is required";
}
if (Validator.isEmpty(data.degree)) {
errors.degree = "Degree field is required";
}
if (Validator.isEmpty(data.fieldofstudy)) {
errors.fieldofstudy = "Field of study field is required";
}
if (Validator.isEmpty(data.from)) {
errors.from = "From date field is required";
}
return {
errors,
isValid: isEmpty(errors)
};
};
问题出在您的模型中。在模型文件夹中创建配置文件架构时,您不小心将其命名为fieldsofstudy
而不是fieldofstudy
(单数(。验证中的字段必须与模型中的字段匹配。