Trying to upload a image to azure storage blob But the fetch method doesn't works
It works properly in postman but shows error in fetch
Not problem in heroku deployment
What is the problem in this code
const uploadImage = async () => {
try {
const Data = new FormData();
Data.append('file', image);
console.log("form data", Data);
await fetch('https://fame-azure.herokuapp.com/aa', {
method: 'POST',
body: Data
})
} catch (err) {
console.log(err);
}
app.post('/aa', upload.any(), function (req, res, next) {
console.log(req.files)
res.send('ok')
res.status(200).send('Uploaded: ' + req.files)
})
This is because in your backend, you are first sending a response with res.send('ok'). After that you are again sending a response with res.status(200).send('Uploaded: ' + req.files), however as the response was already submitted, you cannot update/send it again thus the error. If you simply remove the first line, the error would be gone.
This is because you are trying to send response twice.
app.post('/aa', upload.any(), function (req, res, next) {
console.log(req.files)
// res.send('ok') <-- remove this
res.status(200).send('Uploaded: ' + req.files) // <-- use this
res.json({uploaded: req.files}) // <-- or this, using json() method will set the response headers 200 automatically
})
If u think it looks fine in postman, is because the postman doesnt receive any error response from your server, the error only logged in your server.