I am creating an application and when I try to upload an image it gives this error in the console: (node:23) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client. How can I solve this?
My fetch:
let formData = new FormData();
formData.append('arquivo', document.querySelector('input[name="arquivo"]').files[0])
formData.append('candy', candy)
formData.append('theme', document.getElementById('candytheme').value)
fetch('https://nodemae.herokuapp.com/candy', { method: 'post', headers: {'x-auth-token': window.sessionStorage.getItem('token') }, body: formData}).then
My server side:
async store(req, res) {
let candy= req.body.candy;
let name= uniqid(doce+'-')+'.jpg';
let route= `/img/${doce}/`;
let theme= req.body.theme;
let sampleFile = req.files.arquivo;
sampleFile.mv(`./public${route}${name}`, function(err){
if (err) {
return res.send(err)
}
});
const candy = await Candy.create({
name: name,
candy: candy,
route: route,
theme: theme
});
return res.json(candy);
},
The return res.json (candy); returns this:{"errno":-2,"code":"ENOENT","syscall":"open","path":"./public/img/paomel/paomel-q6zynjrg8w45y.jpg"}