Jimp 错误:未找到匹配的构造函数重载

Posted

技术标签:

【中文标题】Jimp 错误:未找到匹配的构造函数重载【英文标题】:Jimp error: No matching constructor overloading was found 【发布时间】:2020-09-02 21:00:21 【问题描述】:

我正在尝试使用我的机器人创建一个 meme 命令,该命令使用 Jimp 将文本添加到用户使用命令发送的图像上。它可以工作,但是每当某些事情没有按计划进行时(例如,有人没有发送图像,有人没有发送需要应用于图像的文本等),它就会出错并使我的机器人崩溃。这是我的代码:

case "meme":
const [topText, bottomText] = args.slice(1).join(" ").split(",");
msg.channel.startTyping();
if (!args[1]) msg.channel.send("You need to give the text you want to apply to the image!");
Jimp.read(msg.attachments.first(), (err, lenna) => 
  Jimp.loadFont(Jimp.FONT_SANS_128_WHITE).then(font => 
    if (err) console.log(err);
    lenna
      .resize(1280, 1080)
      .quality(100) // set quality
      .print(font, 75, 20, 
        text: topText,
        alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
        alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
      , 1100)
      .print(font, 75, 900, 
        text: bottomText,
        alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
        alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
      , 1100)
      .write("./tmp/" + msg.author.id + ".jpg"); // save
  );
);
for (i = 0; i < (1); i++) 
  setTimeout(function () 
    msg.channel.send(
      files: ["./tmp/" + msg.author.id + ".jpg"]
    )
    msg.channel.stopTyping();
    for (i = 0; i < (1); i++) 
      setTimeout(function () 
        fs.unlinkSync("./tmp/" + msg.author.id + ".jpg")
      , 3 * 1000)
    
  , 3 * 1000)

break;

错误:

(node:32440) UnhandledPromiseRejectionWarning: Error: No matching constructor overloading was found. Please see the docs for how to call the Jimp constructor.
    at Jimp.throwError (C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\@jimp\utils\dist\index.js:35:13)
    at new Jimp (C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\@jimp\core\dist\index.js:502:85)
    at _construct (C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\@babel\runtime\helpers\construct.js:19:21)
    at C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\@jimp\core\dist\index.js:1016:32
    at new Promise (<anonymous>)
    at Function.Jimp.read (C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\@jimp\core\dist\index.js:1015:10)
    at Client.<anonymous> (C:\Users\lqshkiwi\Desktop\Discord Bot\index.js:53:18)
    at Client.emit (events.js:310:20)
    at MessageCreateAction.handle (C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\lqshkiwi\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
(node:32440) 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:32440) [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.
(node:32440) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, stat 'C:\Users\lqshkiwi\Desktop\Discord Bot\tmp\652940695585292299.jpg'
(node:32440) 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: 2)
internal/fs/utils.js:230
    throw err;
    ^

Error: ENOENT: no such file or directory, unlink './tmp/652940695585292299.jpg'
    at Object.unlinkSync (fs.js:1053:3)
    at Timeout._onTimeout (C:\Users\lqshkiwi\Desktop\Discord Bot\index.js:80:32)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7) 
  errno: -4058,
  syscall: 'unlink',
  code: 'ENOENT',
  path: './tmp/652940695585292299.jpg'

【问题讨论】:

错误是什么? pastebin.com/6P6i6CuS 下次请在问题中编辑额外信息 哦,好吧,这是我在这里的第一个问题,所以我不太熟悉礼仪:) 在我的例子中,当我将undefined 传递给Jimp.read 时出现了这个问题。 【参考方案1】:

如果不知道您要处理的具体错误是什么,最好的方法是将您的整个代码部分包装在 try/catch 中。在 catch 部分,您可以console.warn 错误并希望使用日志来调试它并提供更多详细信息。

【讨论】:

我试过了,但是没有用。我在这里有一个错误的 pastebin:pastebin.com/6P6i6CuS【参考方案2】:

如果用户没有遵循正确的命令,您忘记添加return 语句以退出逻辑。当用户没有提供参数时,我猜 msg.attachments.first() 返回 undefined 这就是 Jimp 出错的原因。

if (!args[1]) 
  return msg.channel.send("You need to give the text you want to apply to the image!");
 

try  // it's good to have a try catch when dealing with asynchronous code
  Jimp.read(msg.attachments.first(), (err, lenna) => 
...

【讨论】:

以上是关于Jimp 错误:未找到匹配的构造函数重载的主要内容,如果未能解决你的问题,请参考以下文章

没有匹配的函数用于调用未解析的重载函数类型

派生类中的重载构造函数

每个构造函数,方法和运算符重载的c ++多重定义错误[重复]

绑定 shared_ptr::reset - 找不到匹配的重载函数

构造函数重载类获取链接器错误使用DLL?

错误:在构造函数(C++)中没有重载函数的实例