简体   繁体   中英

(node:1572) UnhandledPromiseRejectionWarning: ValidationError: profile validation failed: education.0.fieldsofstudy: Path `fieldsofstudy` is required

I am trying to find the root cause of this validation error: (node:1572) UnhandledPromiseRejectionWarning: ValidationError: profile validation failed: education.0.fieldsofstudy: Path fieldsofstudy is required.
This is happening even I changed the "fieldsofstudy" to some other names, such as "majors". The same error message will still return.

Here is my profile code for education

// @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));
  });
 }
);

Here is my code for the education tab

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)
 };
};

The problem is in your model. When you are creating your profile schema in your models folder, you have accidentally named it fieldsofstudy instead of fieldofstudy (singular). The fields in the validation has to match the fields in the model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM