I'm currently creating a website using MEAN stack, and I'm almost done, but there's a problem with the "user editing his/her profile" part. When the user edits anything ALONG WITH editing his/her birthdate, the edit works just fine and all is updated in the database, however when the birthdate field is left empty I get the following error in node:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ValidationError: CastError: Cast to Date failed for value "Invalid Date" at path "birthdate"
Here's my nodejs code:
exports.editInformation = function(req, res) {
var id = req.params.userID;
var body = req.body;
User.findOne({_id:id}, function(err, user) {
if(err)
res.send("error");
else {
if(!user)
res.send("user not found");
else {
if(body.name)
user.name = body.name;
if(body.birthdate)
user.birthdate = new Date(body.birthdate);
if(typeof body.phone != "undefined" && body.phone.length > 0)
user.phone = body.phone;
if(typeof body.gender != "undefined" && body.gender.length > 0)
user.gender = body.gender;
if(typeof body.address != "undefined" && body.address.length > 0)
user.address = body.address;
if(typeof body.email != "undefined" && body.email.length > 0)
user.email = body.email;
if(typeof file != "undefined")
user.profilePic = file.filename;
user.save();
}
}
});
}
via thelili700
No comments:
Post a Comment