未处理的承诺拒绝与'if else'指令和日期

问题描述 投票:0回答:1

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

我正在测试(使用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()中用“if”更改“else if”,则会显示:

entering isFuture function
after declaring today

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

所以:

  1. 为什么“if”和“else if”在tellDate()有所作为?
  2. 日期比较有什么问题?
javascript node.js discord.js
1个回答
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;
}
© www.soinside.com 2019 - 2024. All rights reserved.