I am sending excel in formdata() property from reactJs as follows
let formData = new FormData(), headers = {};
headers['Content-Type'] = 'multipart/form-data';
headers['Accept'] = 'application/json';
formData.append('dataFile', file);
let res = await axios({
method: 'POST',
headers,
url: '/api/products/general-excel-import',
data: formData
});
At my backend route is defined as
api.post(
"/api/products/general-excel-import",
[multer.any()],
productController.generalExcelImport
);
And my controller function:
const excelToJson = require('convert-excel-to-json');
exports.generalExcelImport = async function(req, res) {
const file = req.files[0];
console.log({ file: file});
const jsonData = excelToJson({sourceFile: file.buffer });
return res.status(200).send({ data: jsonData, message: 'Imported successfully' });
}
The console.log in controller outputs the following object in terminal
file: {
fieldname: 'dataFile',
originalname: 'findings.xlsx',
encoding: '7bit',
mimetype: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
buffer: <Buffer 50 4b 03 04 14 00 06 00 08 00 00 00 21 00 62 ee 9d 68 5e 01 00 00 90 04 00 00 13 00 08 02 5b 43 6f 6e 74 65 6e 74 5f 54 79 70 65 73 5d 2e 78 6d 6c 20 ... 9091 more bytes>,
size: 9141
}
when the request is made at backend route it throws the following error
[0] TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of
type string or an instance of Buffer or URL. Received an instance of
Object
can any one tell where i'm wrong in code