【问题标题】:Discord.js: (node:147) UnhandledPromiseRejectionWarning: Error: timeout of 1000ms exceededDiscord.js:(节点:147)UnhandledPromiseRejectionWarning:错误:超过 1000 毫秒的超时
【发布时间】:2021-11-24 01:11:08
【问题描述】:

最近由于某种未知原因,我开始在控制台中看到常规错误。像这样:

(node:147) UnhandledPromiseRejectionWarning: Error: timeout of 1000ms exceeded
    at createError (/home/runner/ricebot/node_modules/axios/lib/core/createError.js:16:15)
    at RedirectableRequest.handleRequestTimeout (/home/runner/ricebot/node_modules/axios/lib/adapters/http.js:280:16)
    at RedirectableRequest.emit (events.js:314:20)
    at RedirectableRequest.EventEmitter.emit (domain.js:483:12)
    at Timeout._onTimeout (/home/runner/ricebot/node_modules/follow-redirects/index.js:166:12)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)

它有时也会使我的机器人的延迟达到 30000 毫秒。

我的有趣命令中只有 Axios 部分,这是其中之一(但它仍然可以正常工作,只是记录错误):

const url = 'https://no-api-key.com/api/v2/animals/cat';

        let image;
        let fact;
        try {
            const { data } = await axios.get(url);
            console.log(data);
            image = data.image;
            fact = data.fact;
        } catch (e) {
      console.log(e)
            return message.channel.send('An error occured, please try again!');
        }

这不是过去的事情。

【问题讨论】:

  • 好像axios无法获取你的url
  • @ChristophBlüm 它明白了,axios 部分工作
  • @8less 请您提供更多代码,因为您正在使用 await 您在一个函数中,console.log(data); 没有显示什么?
  • @Saren 它记录了它应该记录的所有内容:{ 事实:'迪士尼乐园“雇佣”了近 200 只猫。',图片:'no-api-key.com/image/cat/95147.png'}
  • 也许尝试在之后对其进行解构? const a = await axios.get(url); const { data } = a

标签: javascript node.js axios discord.js bots


【解决方案1】:

由于您的 axios 调用没有捕获,因此出现此错误或警告是正常的。

管理 axios 请求以防止此类问题非常重要。另外,我假设您展示的示例缺少一些数据,因为您不需要在 catch 状态之外定义结果变量,所以我假设您有一个循环或其他可能导致这种情况的东西。

如果您在循环中或使用 Promise.all 调用同一端点,有时您需要限制并发请求。如果不是,请忽略这部分。

首先,确保正确设置您的axios.default.timeout 值,以定义在有响应时取消请求的时间。

根据您的要求定义响应状态。

function checkStatus(response) {
  if (response.status >= 200 && response.status < 300) {
    return response;
  }
  const error: any = new Error(response.statusText);
  error.response = response;
  throw error;
}

定义解析代码以确保您始终具有相同的结构。

function parseJSON(response) {
  return response.data;
}

定义一个 catch 功能来检查错误并决定是抛出错误还是只记录它们。

function catchError(e){
  console.error(e);
  // TODO send a message
  return null;
}

让我们通过 .catch 在一次调用中使用它们。

const myResult = await axios.get(url)
    .then(checkStatus)
    .then(parseJSON)
    .catch(catchError);
console.log(myResult);

【讨论】:

  • 嘿,这是一个很好的答案,但我的代码中没有任何 axios 部分,因为我将其注释掉了,它仍然会发送这些错误
  • @8less 请在此处更新您的代码。如果没有 axios 怎么会出现这个错误? ` 在 RedirectableRequest.emit (events.js:314:20) 上的 RedirectableRequest.handleRequestTimeout (/home/runner/ricebot/node_modules/axios/lib/adapters/http.js:280:16) `
  • axios 本身已安装,但在任何地方都不需要并使用
【解决方案2】:

如果我没记错的话,您不能直接从 Promise 访问属性,因为从属性声明变量是同步的,而您的 url() 方法是异步的。
axios.get(url) 的数据类型是 Promise,当没有返回响应时,它没有您早期访问的 data 属性。因此,您必须等待响应。当返回响应时,该方法的数据类型将更改为预期的类型。那时您可以访问 data 属性 所以我认为你的代码应该是这样的:

const url = 'https://no-api-key.com/api/v2/animals/cat';
let image;
let fact;
try {
    const axiosUrl = await axios.get(url);
    const data = axiosUrl.data;
    console.log(data);
    image = data.image;
    fact = data.fact;
} catch (e) {
    console.log(e)
    return message.channel.send('An error occured, please try again!');
}

假设这段代码在异步上下文中,它应该可以正常工作。
我希望这个答案对你有用。

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2022-06-14
    • 1970-01-01
    相关资源
    最近更新 更多