【问题标题】:UnhandledPromiseRejectionWarning when date is left blank日期留空时出现 UnhandledPromiseRejectionWarning
【发布时间】: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


    【解决方案1】:

    错误信息说明了原因:

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

    它建议 body.birthdate 计算结果为 true(也许它包含一个空格或什么?),但 new Date(body.birthdate) 产生一个无效的日期。

    因为您没有处理user.save() 抛出的任何 错误,所以您会得到UnhandledPromiseRejectionWarning。您也没有发回任何响应。

    所以有两个问题需要解决:在调用new Date(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("Birthdate in body:" + body.birthdate); .. 这显示在控制台中: Birthdate in body: null @robertklep
    • 我的猜测是它是一个包含单词null的字符串,这会导致创建的日期无效。您需要验证您的输入,或过滤掉这些字符串 (if (body.birthdate && body.birthdate !== 'null') { ... })。
    猜你喜欢
    • 2021-08-05
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    相关资源
    最近更新 更多