【问题标题】:(node:23042) UnhandledPromiseRejectionWarning: Error: Could not find MIME for Buffer <null>(节点:23042)UnhandledPromiseRejectionWarning:错误:找不到缓冲区的 MIME <null>
【发布时间】:2020-08-29 07:09:57
【问题描述】:

我正在尝试在 Typescript 中使用 express 获取图像 URL,对其进行过滤并将图像作为响应发回。这是端点:

app.get("/filteredimage/", async (req, res) =>{
try{
  let {image_url} = req.query;
  if(!image_url){
    return res.status(400).send("bad request!");
  }
  console.log(image_url);
  const path = await filterImageFromURL(image_url);
  res.sendFile(path);
  res.on('finish', () => deleteLocalFiles([path]));
} catch {
  return res.status(500).send({error: 'Unable to process your request'});
}
});

过滤图片url函数为:

export async function filterImageFromURL(inputURL: string): Promise<string>{
return new Promise( async resolve => {
    const photo = await Jimp.read(inputURL);
    const outpath = '/tmp/filtered.'+Math.floor(Math.random() * 2000)+'.jpg';
    await photo
    .resize(256, 256) // resize
    .quality(60) // set JPEG quality
    .greyscale() // set greyscale
    .write(__dirname+outpath, (img)=>{
        resolve(__dirname+outpath);
    });
});
}

但是在到达端点时,我得到以下错误:

(node:23042) UnhandledPromiseRejectionWarning: Error: Could not find MIME for Buffer <null>
at Jimp.parseBitmap (/home/hades/udacity/cloud-developer/course-02/project/image-filter-starter-code/node_modules/@jimp/core/src/utils/image-bitmap.js:73:15)
at Jimp.call [as parseBitmap] (/home/hades/udacity/cloud-developer/course-02/project/image-filter-starter-code/node_modules/@jimp/core/src/index.js:395:17)
at parseBitmap (/home/hades/udacity/cloud-developer/course-02/project/image-filter-starter-code/node_modules/@jimp/core/src/index.js:339:14)
at cb (/home/hades/udacity/cloud-developer/course-02/project/image-filter-starter-code/node_modules/@jimp/core/src/index.js:68:14)
at cb (/home/hades/udacity/cloud-developer/course-02/project/image-filter-starter-code/node_modules/@jimp/core/src/request.js:47:9)
at IncomingMessage.<anonymous> (/home/hades/udacity/cloud-developer/course-02/project/image-filter-starter-code/node_modules/phin/lib/phin.compiled.js:1:2038)
at IncomingMessage.emit (events.js:322:22)
at endReadableNT (_stream_readable.js:1187:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:23042) 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:23042) [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.

尝试在两个函数中添加 try-catch 块,但没有成功。我错过了什么?

【问题讨论】:

    标签: javascript node.js typescript express jimp


    【解决方案1】:

    我正在做同样的课程,问题是示例图片 (https://timedotcom.files.wordpress.com/2019/03/kitten-report.jpg) 不再存在。一个简洁的错误处理需要修改提供的 util 代码:

    export async function filterImageFromURL(inputURL: string): Promise<string> {
        return new Promise((resolve, reject) => {
            Jimp.read(inputURL).then(photo => {
                const outpath = '/tmp/filtered.' + Math.floor(Math.random() * 2000) + '.jpg';
                photo
                    .resize(256, 256) // resize
                    .quality(60) // set JPEG quality
                    .greyscale() // set greyscale
                    .write(__dirname + outpath, (img) => {
                        resolve(__dirname + outpath);
                    });
            }).catch(err => {
                console.error(err);
                reject("Could not read image.");
            })
        });
    }
    

    通过此修改,您可以对代码片段中的此错误做出反应。

    【讨论】:

      【解决方案2】:

      在调用 Jimp.read() 时似乎没有捕获到错误。

      请尝试以下 sn-p。

          return new Promise(async (resolve, reject) => {
              try {
                  const photo = await Jimp.read(inputURL);
                  const outpath = '/tmp/filtered.' + Math.floor(Math.random() * 2000) + '.jpg';
                  await photo
                      .resize(256, 256) // resize
                      .quality(60) // set JPEG quality
                      .greyscale() // set greyscale
                      .write(__dirname + outpath, (img) => {
                          resolve(__dirname + outpath);
                      });
      
              } catch (error) {
                  reject(error)
              }
          });
      }
      

      【讨论】:

        猜你喜欢
        • 2021-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-16
        • 2020-09-15
        • 2017-06-19
        • 1970-01-01
        • 2022-12-15
        相关资源
        最近更新 更多