Wherever I am making a post request using postman to localhost:5000/api/profile/experience
I am getting these warning
UnhandledPromiseRejectionWarning: ValidationError: Profile validation failed: experience.0.title: Path `title` is required., experience.0.company: Path `company` is required., experience.0.from: Path `from` is required.
and also I am not getting error message saying that title, company, from values are required eventhough I have not field those field. Here my validation js file
const Validator = require('validator');
const isEmpty = require('./is-empty');
module.exports = function validateExperienceInput(data){
let errors = {};
data.title = !isEmpty(data.title) ? data.title : '';
data.company = !isEmpty(data.company) ? data.company : '';
data.from = !isEmpty(data.from) ? data.from : '';
if(Validator.isEmpty(data.title)){
errors.title = 'Title field is required'
}
if(Validator.isEmpty(data.company)){
errors.company = 'company field is required'
}
if(Validator.isEmpty(data.from)){
errors.from = 'From field is required'
}
return {
errors,
isValid: isEmpty(errors)
}
}
Here is the router file
router.post('/experience', passport.authenticate('jwt',{session: false}), (req,res) => {
const {errors, isValid} = validateExperienceInput(req.body);
Profile.findOne({user:req.user.id})
.then(profile => {
const newExp = {
title: req.body.title,
company: req.body.company,
location: req.body.location,
from: req.body.from,
to: req.body.to,
current: req.body.current,
description: req.body.description
}
// Add to exp array
profile.experience.unshift(newExp)
profile.save().then(profile => res.json(profile))
})
})
What am I missing?
You need to add a catch()
(rejection handler) to findOne()
to handle any errors/rejections occurring from findOne()
. From the Node.js Process documentation for unhandledrejection :
The 'unhandledRejection' event is emitted whenever a Promise is rejected and no error handler is attached to the promise within a turn of the event loop. When programming with Promises, exceptions are encapsulated as "rejected promises". Rejections can be caught and handled using promise.catch() and are propagated through a Promise chain. The 'unhandledRejection' event is useful for detecting and keeping track of promises that were rejected whose rejections have not yet been handled.
router.post(
"/experience",
passport.authenticate("jwt", { session: false }),
(req, res) => {
const { errors, isValid } = validateExperienceInput(req.body);
Profile.findOne({ user: req.user.id })
.then(profile => {
const newExp = {
title: req.body.title,
company: req.body.company,
location: req.body.location,
from: req.body.from,
to: req.body.to,
current: req.body.current,
description: req.body.description
};
// Add to exp array
profile.experience.unshift(newExp);
profile.save().then(profile => res.json(profile));
})
.catch(err => {
// do something with error here such send error message or logging
// res.json(err);
});
}
);
Basically add a catch()
anytime you have a then()
to handle any errors rejections.
Hopefully that helps!
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.