【问题标题】:Could not interpret "{}" as string无法将“{}”解释为字符串
【发布时间】:2020-12-15 22:10:21
【问题描述】:

所以我正在开发一个不和谐的机器人,我想发出一个命令,当用户键入“.doggo”时,会出现一条狗的随机图像。这是我当前命令的代码。我已经在应用程序中安装了 node-fetch 和 discord.js 库。

else if(command === 'doggo') {
    const Fetch = fetch('https://dog.ceo/api/breeds/image/random');
    const embed = new Discord.MessageEmbed()
    .setTitle('doggo :dog:')
    .setImage(Fetch);
    message.channel.send(embed);
}

由于某种原因,我在执行命令时收到此警告消息:

(node:10416) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
embed.image.url: Could not interpret "{}" as string.
    at RequestHandler.execute (C:\Users\jnett\Desktop\demon\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:10416) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:10416) [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.

我的代码有什么问题?我需要做什么来修复它?

【问题讨论】:

  • fetch() 是异步的并返回一个承诺
  • 那个 api 返回什么?

标签: javascript node.js node-fetch


【解决方案1】:

fetch() 调用是异步的,如果不这样处理会导致问题。在此处阅读有关 Fetch API 的更多信息:

https://developers.google.com/web/updates/2015/03/introduction-to-fetch

关于错误信息...

  1. Invalid Form Body 是由于尝试嵌入您的节点应用尚未收到的图像。这可以通过在 .then(() => { }) 回调中捕获使用来自 fetch 调用的数据的后续代码来避免。

  2. UnhandledPromiseRejectionWarningDeprecationWarning(从技术上讲是第一个错误)是由于缺少 .catch() 回调。您可以在此处指定如果无法满足提取请求时要执行的操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 2020-09-14
    • 1970-01-01
    相关资源
    最近更新 更多