【发布时间】:2017-09-16 20:55:30
【问题描述】:
我目前正在使用 MEAN 堆栈创建一个网站,几乎完成了,但是“用户编辑他/她的个人资料”部分存在问题。当用户在编辑他/她的生日的同时编辑任何内容时,编辑工作正常并且数据库中的所有内容都会更新,但是当生日字段为空时,我在节点中收到以下错误:
UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:1):ValidationError:CastError:Cast to Date 在路径“birthdate”处的值“Invalid Date”失败
这是我的 nodejs 代码:
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();
}
}
});
}
【问题讨论】:
标签: javascript angularjs node.js mongoose mean-stack