I am using mv module from nodeJs to move a file from the current location to other.
Like this:
Plant.find({
where: {
id: plantId
}
}).then(function (plant) {
Foto.find({
where: { id: fotoId, plantId: null },
}).then(function (foto) {
currentPath = __dirname + "/../public/" + foto.image;
pathToSave = foto.image;
pathToSave = pathToSave.replace("unknown", plant.specie);
newPath = __dirname + "/../public/" + newPath;
mv(currentPath, newPath, function (err) {
return res.status(400).json({ error: "failed to move file" });
});
Foto.update({ plantId: plantId, image: pathToSave }, {
where: { id: fotoId }
}).then(function (result) {
return res.status(200).json({ message: "plant identified with success" });
}).catch(function (error) {
return res.status(400).json({ error: "failed to identiy plant" })
})
}).catch(function (error) {
return res.status(400).json({ error: "Foto not found" })
})
}).catch(function (err) {
return res.status(400).json({ error: "The plant requested doesn't exist" });
})
important part:
mv(currentPath, newPath, function (err) {
return res.status(400).json({ error: "failed to move file" });
});
the problem is that i get this error: Can't set headers after they are sent., i think the issue is related to the fact that the mv function doesn't have a success calback so what i do inside is syncronous and in breaks with the return statements.
Any tip about this?
Thank you very much
via Filipe Costa
No comments:
Post a Comment