(节点:1572)UnhandledPromiseRejectionWarning:ValidationError:概要文件验证失败:Education.0.fieldsofstudy:需要路径`fieldsofstudy`

时间:2018-08-29 06:14:30

标签: javascript node.js mongodb mongoose

我正在尝试查找此验证错误的根本原因:(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)
 };
};

1 个答案:

答案 0 :(得分:1)

问题出在您的模型中。在模型文件夹中创建概要文件架构时,您意外地将其命名为fieldsofstudy而不是fieldofstudy(单数)。验证中的字段必须与模型中的字段匹配。