I want to send a file as well as JSON data from a form This is my react handle submission method.
const formData = new FormData();
formData.append('File', selectedFile);
const data = {
name: 'vasu',
email: '[email protected]',
role: 'student'
}
formData.append('data', data);
axios.post('http://localhost:8000/users', formData ).then((result) => {
console.log('Success:', result);
})
.catch((error) => {
console.error('Error:', error);
});
And this Is my server.js method
app.post('/users', upload.single('File') , async (req, res) => {
console.log(req.file , req.body);
res.send(req.body);
})
and this is the output I get
{
fieldname: 'File',
originalname: '4eec81ebddc991f6ff017e600dbf58ac.png',
encoding: '7bit',
mimetype: 'image/png',
destination: 'avatars',
filename: 'File-4eec81ebddc991f6ff017e600dbf58ac.png-1620467341921',
path: 'avatars\\File-4eec81ebddc991f6ff017e600dbf58ac.png-1620467341921',
size: 426281
} [Object: null prototype] { data: '[object Object]' }
i don't want this [Object: null prototype] { data: '[object Object]' } instead i want the JSON object i send i am also using
app.use(express.json())
app.use(express.urlencoded({extended: true}))
I am also using multer for file upload