开发者

Error "Unexpected end of multipart form" in busboy file upload

开发者 https://www.devze.com 2022-12-07 22:46 出处:网络
I am using busboy to upload file in node/express app. I get error Unexpected end of multipart form and the application crash. Whenever I try the route on insomnia, nothing works. Before it was showing

I am using busboy to upload file in node/express app. I get error Unexpected end of multipart form and the application crash. Whenever I try the route on insomnia, nothing works. Before it was showing that Busboy is not a constructor, so I changed let to const and deleted new, leaving it as it is in the code.

What could be the cause of this error?

this is my code:

router.post('/', async (req, res) => {
    const busboy = Busboy({ headers: req.headers });
    busboy.on('finish', async () => {
        try {
            const { salaoId, servicos } = req.body;
            let error = [];
            let arquivos = [];

            console.log(req.files)

            if (req.files && Object.keys(req.files) > 0) {
                for (let key of Object.keys(req.files)) {
                    const file = req.files[key];

                    const nameParts = file.name.split('.');
                    const fileName = `${new Date().getTime()}.${nameParts[nameParts.length - 1]
                        }`;
                    const path = `servicos/${salaoId}/${fileName}`;

                    const response = await aws.uploadToS3(file, path);

                    if (response.error) {
                        error.push({ error: true, message: response.message })
                    } else {
                        arquivos.push(path)
                    }
                }
            }

            if (error.length > 0) {
                res.json(error[0]);
                return false;
            }

            //CRIAR SERVIÇO
            let jsonServico = JSON.parse(servicos);
            const servicoCadastrado = await Servicos(jsonServico).save();

            //CRIAR ARQUIVO
            arquivos = arquivos.map(arquivo => ({
                referenciaId: servicoCadastrado._id,
                model: 'Servicos',
                caminho: arquivo,
            }));

            await Arquivo.insertMany(arquivos);
            res.json({ servicos: servicoCadastrado, arquivos });

        } catch (error) {
            res.json({ error: true, message: error.message })
        }
    });
    req.pipe(req.busboy)
});

module.exports = router;

this is error:

throw er; // Unhandled 'error' event
      ^

Error: Unexpected end of form
    at Multipart._final (C:\Users\Lenovo\Desktop\projetos\meus-projetos\app-claudiagomes\ws\node_modules\busboy\lib\types\mult开发者_如何学Goipart.js:588:17)
0

精彩评论

暂无评论...
验证码 换一张
取 消