Node.js NodeJS返回未处理的承诺拒绝错误

Node.js NodeJS返回未处理的承诺拒绝错误,node.js,discord,Node.js,Discord,我目前的代码是: const Discord = require('discord.js'); const client = new Discord.Client(); client.on('ready', () => { console.log('I am ready!'); }); function getData(location1) { var getResult = ""; request.get('https://maps.googleapis.com/...

我目前的代码是:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  console.log('I am ready!');
});

function getData(location1) {
  var getResult = "";
  request.get('https://maps.googleapis.com/... '  + location1, (err, response) => {
  ...

  return getResult;
}

client.on('message', message => {
    if (message.content === 'ping') {
      message.channel.send('pong');
    }
    else if (message.content === 'query Melbourne') {
      message.channel.send( getData('Melbourne') ) ;
    }
} );
第一个
message.channel.send('pong')
工作正常。但是第二条message.channel.send会继续返回结果:

(node:16047) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): DiscordAPIError: Cannot send an empty message
(node:16047) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
只是一个新手在节点,所以任何指导将不胜感激

----以下更新的代码仍然以相同的方式出现错误:

  var PTest = function () {
      return new Promise(function (resolve, reject) {
          message.channel.send( getData('Melbourne') ) ;
      });
  }
  var myfunc = PTest();
  myfunc.then(function () {
       console.log("Promise Resolved");
  }).catch(function () {
       console.log("Promise Rejected");
  });

您只需发送一条空消息,就像警告消息写入
(节点:16047)未处理的PromisejectionWarning:未处理的承诺拒绝(拒绝id:1):DiscordAPIError:无法发送空消息一样

函数
message.channel.send(getData('Melbourne'))中的
getData('Melbourne')
返回空消息,承诺被拒绝。由于您没有处理承诺拒绝,node.js会在控制台上写入警告,告知您此情况,以防止由于忽略处理承诺拒绝而可能发生的一些潜在异常情况

要处理承诺拒绝,请为此
发送
函数调用
.catch
方法

在更新的代码中,您创建了自己的承诺


其中,
message.channel.send
方法返回的承诺仍未处理。您应该编写
message.channel.send().catch(err=>console.log(err))

您只需发送一条空消息,就像警告消息写入
(节点:16047)未处理的PromisejectionWarning:未处理的承诺拒绝(拒绝id:1):DiscordAPIError:无法发送空消息一样

函数
message.channel.send(getData('Melbourne'))中的
getData('Melbourne')
返回空消息,承诺被拒绝。由于您没有处理承诺拒绝,node.js会在控制台上写入警告,告知您此情况,以防止由于忽略处理承诺拒绝而可能发生的一些潜在异常情况

要处理承诺拒绝,请为此
发送
函数调用
.catch
方法

在更新的代码中,您创建了自己的承诺


其中,
message.channel.send
方法返回的承诺仍未处理。您应该编写
message.channel.send().catch(err=>console.log(err))

我尝试在使用.catch方法找到模式后更改代码。这似乎并没有解决问题。请参阅上面更新的代码,我确实尝试记录结果值。它不是空的。因此,我相信我没有发送空消息。关于空消息的假设只是基于警告文本
DiscordAPIError:Cannot send a empty message
,因此我发现消息看起来是空的,因为似乎在getData()完成之前调用了.send()。如果在getData()上使用console.log,它不会返回空。所以我需要使getData()同步吗?如果
getData
返回承诺,则可以编写
getData()。然后(msg=>message.channel.send(msg)).catch(err=>console.log(err))
我试图在使用.catch方法找到的模式之后更改代码。这似乎并没有解决问题。请参阅上面更新的代码,我确实尝试记录结果值。它不是空的。因此,我相信我没有发送空消息。关于空消息的假设只是基于警告文本
DiscordAPIError:Cannot send a empty message
,因此我发现消息看起来是空的,因为似乎在getData()完成之前调用了.send()。如果在getData()上使用console.log,它不会返回空。那么我是否需要以某种方式使getData()同步?如果
getData
返回承诺,那么您可以编写
getData()。然后(msg=>message.channel.send(msg)).catch(err=>console.log(err))
return new Promise(function (resolve, reject) {
      message.channel.send( getData('Melbourne') ) ;
  });