Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 日期留空时未处理PromisejectionWarning_Javascript_Angularjs_Node.js_Mongoose_Mean Stack - Fatal编程技术网

Javascript 日期留空时未处理PromisejectionWarning

Javascript 日期留空时未处理PromisejectionWarning,javascript,angularjs,node.js,mongoose,mean-stack,Javascript,Angularjs,Node.js,Mongoose,Mean Stack,我目前正在使用MEAN stack创建一个网站,我几乎完成了,但“用户编辑其个人资料”部分存在问题。当用户在编辑他/她的出生日期的同时编辑任何内容时,编辑工作正常,数据库中的所有内容都会更新,但是当“出生日期”字段保留为空时,节点中会出现以下错误: 未经处理的PromisejectionWarning:未经处理的承诺拒绝(拒绝id:1):ValidationError:CastError:路径“birthdate”处的值“Invalid Date”的转换到日期失败 以下是我的nodejs代码:

我目前正在使用MEAN stack创建一个网站,我几乎完成了,但“用户编辑其个人资料”部分存在问题。当用户在编辑他/她的出生日期的同时编辑任何内容时,编辑工作正常,数据库中的所有内容都会更新,但是当“出生日期”字段保留为空时,节点中会出现以下错误:

未经处理的PromisejectionWarning:未经处理的承诺拒绝(拒绝id:1):ValidationError:CastError:路径“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();
                }
            }
        });
}

错误消息说明了原因:

ValidationError:CastError:Cast to Date路径“birthdate”处的值“Invalid Date”失败

它建议
body.birthdate
计算为
true
(可能它包含空格或什么?),但
newdate(body.birthdate)
生成无效日期

由于您没有处理由
user.save()
引发的任何错误,因此会收到
未处理的PromisejectionWarning
。你也没有回复

因此,有两个问题需要解决:在调用
新日期(body.birthdate)
后,您应该检查该日期是否实际有效:

if (body.birthdate) {
  user.birthdate = new Date(body.birthdate);
  if ( isNaN( user.birthdate.getTime() ) ) {
    return res.send('error'); // or whatever response you deem appropriate
  }
}
此外,您还应该处理
save
引发的错误,并采取相应的行动:

user.save(function(err) {
  if (err) return res.send('error');
  res.send('okay');
});

好了,现在控制台中没有显示错误,但是,这仍然没有解决问题。也就是说,如果用户编辑了自己的姓名,而没有更改出生日期(保留为html预览的日期),则数据库中的用户信息(在本例中为姓名)不会得到更新。那么
body.birthdate
到底包含了什么?您是否可以将其传递到
new Date()
(也就是说,它是
Date
构造函数接受的格式)?当我不在编辑生日字段中输入任何内容时,当我在console.log中输入以下内容时:console.log(“body中的生日:+body.birthdate”)。。这显示在控制台中:body:null@robertklepMy中的Birthdate:null我猜这是一个包含单词
null
的字符串,这将导致创建的日期无效。您需要验证输入,或者过滤掉那些字符串(
if(body.birthdate&&body.birthdate!='null'){…}
)。