我正在学习 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] 进程强制结束。
后端似乎不知道请求何时结束,因此强制终止上传过程。不知道从这里去哪里,所以任何帮助将不胜感激。