I'm trying to upload some files with AWS using the busboy module, but when I use req.pipe to say that busboy is controlling the response, I get the error:
req.pipeis not a function.
Here is my code:
const uploadDeArquivos = async (reqHeaders, reqFiles, reqBody, reqPipe) => {
let bb = busboy({ headers: reqHeaders });
bb.on('finish', async () => {
const { salaoId, servico } = reqBody;
let errors = [];
let arquivos = [];
if(reqFiles && Object.keys(reqFiles).length > 0) {
for (let key of Object.keys(reqFiles)) {
const file = reqFiles[key];
const splitName = file.name.split('.');
const fileName = `${new Date().getTime()}.${
splitName[splitName.length - 1]
}`;
const path = `servicos/${salaoId}/${fileName}`;
const response = await aws.uploadToS3(file, path);
response.error
? errors.push({ error: true, message: response.message })
: arquivos.push(path);
};
};
if (errors.length > 0) throw errorThrow(500, errors[0]);
// Criar Serviço
let jsonServico = JSON.parse(servico);
const novoServico = await Servico(jsonServico).save();
// Criar arquivo
arquivos = arquivos.map(arquivo => ({
referenciaId: novoServico._id,
model: 'Servico',
caminho: arquivo,
}));
await Arquivo.insertMany(arquivos);
return {
servico: novoServico,
arquivos
};
});
reqPipe(bb);
};
const newServico = async (req, res, next) => {
try{
const newServico = await uploadDeArquivos(req.headers, req.files, req.body, req.pipe);
return res.status(201).json(newServico);
} catch(error){
next(error);
}
};
router.post('/', newServico);