未处理的承诺拒绝,带有“ if else”说明和日期

时间:2018-11-29 11:30:56

标签: javascript node.js discord.js

更新项目的节点模块后发生新的执行错误。

我正在使用DiscordJS测试以下代码:

if (msg.content === "!next") {
    msg.channel.send(tellDate()) ;
}

tellDate()为:

function tellDate() {
    var myDate = ...// Initialize myDate as a correct Date
    if(isPast(myDate){
        // ...
    }
    else if(isFuture(myDate)){
        console.log("after call isFuture function");
        return `tellDate : test test`;
    }
}

myDate就像进入isFuture()测试一样。这是isFuture()

function isFuture(d)
{
    console.log("entering isFuture function");
    const today = new Date();
    console.log("after declaring today");
    if(d.getFullYear() > today.getFullYear())
        return true;
    else if(d.getFullYear() === today.getFullYear() && d.getMonth() > today.getMonth())
        return true;
    else if(d.getFullYear() === today.getFullYear() && d.getMonth() === today.getMonth() && d.getDate() > today.getDate())
        return true;
    else
        return false;
}

但是执行从未达到isFuture()

(node:8380) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejec
tion id: 2): DiscordAPIError: Cannot send an empty message
(node:8380) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre
cated. In the future, promise rejections that are not handled will terminate the
 Node.js process with a non-zero exit code.

但是,如果我在tellDate()中将“ else if”改为“ if”,则会显示:

entering isFuture function
after declaring today

但是它不会执行日期比较(相同的错误)。

所以:

  1. 为什么“ if”和“ else if”在tellDate()中有所不同?
  2. 日期比较出了什么问题?

1 个答案:

答案 0 :(得分:0)

您通过msg.channel.send发送的邮件不能为空

if (msg.content === "!next") {
    const message = tellDate();
    if(message) msg.channel.send(message) ;
}

您可以简单地使用>来比较日期

function isFuture(d)
{
    console.log("entering isFuture function");
    const today = new Date();
    console.log("after declaring today");
    return d > today;
}