0

我正在学习 Fastify,所以我编写了一个简单的程序来使用 Fastify-Formidable 上传文件。该文件已成功上传并在 mv 包的帮助下移动到其目标目录。然而,当这种情况发生时,Fastify 会在控制台中抛出一个未处理的 Promise 错误。我的代码是:

const insertManyWorkers = async (request, reply) => {      
  try {
    await request.parseMultipart();    
    let oldpath = await request.files.picture.path;
    let uploadDir = '/home/hamza/Documents/Web-Projects/Personal/test/img/' + request.files.picture.name;   
    await mv(oldpath, uploadDir, {
      mkdirp: true
    }, function(err) {    
      if (err) {
        reply.send(err);    
      } else {
        reply.code(200).send('uploaded')    
      }
    });   
  } catch (error) {
    console.log(error);
    reply.send(error)    
  };
};

错误如下:

01:17:24 ✨ incoming request POST xxx /workers/insert 01:17:24  Promise may not be fulfilled with 'undefined' when statusCode is not 204 FastifyError: Promise may not be fulfilled with 'undefined' when statusCode is not 204 at /home/user-1/Documents/Web-Projects/test/node_modules/fastify/lib/wrapThenable.js:30:30 at processTicksAndRejections (node:internal/process/task_queues:96:5) { "err": { "type": "FastifyError", "message": "Promise may not be fulfilled with 'undefined' when statusCode is not 204", "stack": "FastifyError: Promise may not be fulfilled with 'undefined' when statusCode is not 204\n    at /home/hamza/Documents/Web-Projects/Personal/test/node_modules/fastify/lib/wrapThenable.js:30:30\n at processTicksAndRejections (node:internal/process/task_queues:96:5)", "name": "FastifyError", "code": "FST_ERR_PROMISE_NOT_FULFILLED", "statusCode": 500 } } 01:17:24 ✨ request completed 18ms [fastify-cli] process forced end 01:17:30 ✨ Server listening at http://0.0.0.0:5000

此外,Fastify 还会在文件上传几毫秒后记录[fastify-cli] 进程强制结束。

后端似乎不知道请求何时结束,因此强制终止上传过程。不知道从这里去哪里,所以任何帮助将不胜感激。

4

1 回答 1

0

当使用异步路由处理程序(insertManyWorkers在你的情况下)时,Fastify 期望处理程序返回的 Promise 使用定义的值(将作为回复正文发送)解析,除非你明确地将回复状态代码设置为 204,这意味着“否内容”。

另一个问题是您的代码正在等待mv哪个基于回调的函数不返回承诺。为了在函数内部调用mvwith ,你可以使用 promisify:awaitasync

const mv = require('mv');
const util = require('util');
const mvPromisified = util.promisify(mv);

此外,除非您打算对可能捕获的错误做任何有意义的事情,否则您可以摆脱该try/catch块,以便处理函数中抛出的任何错误都被 Fastify 捕获(并且可能由 Pino 记录,具体取决于您的日志级别设置),然后响应代码 500。因此,您的insertManyWorkers函数可能如下所示:

const insertManyWorkers = async (request, reply) => {      
  await request.parseMultipart();    
  let oldpath = await request.files.picture.path;
  let uploadDir = '/home/hamza/Documents/Web-Projects/Personal/test/img/' + request.files.picture.name;   
  await mvPromisified(oldpath, uploadDir, {
    mkdirp: true
  })
  return 'uploaded';
};

还值得注意的是awaitinlet oldpath = await request.files.picture.path是不必要的,因为request.files.picture.path它只是一个属性访问,而不是一个返回可以等待的承诺的函数的调用。

于 2022-01-02T14:59:56.777 回答