I have a Firebase Functions that I am unable to query from my React.js web app but no problem through Postman.
When I try to make a request via the web app I get the following error in my Firebase Console:
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (/worker/node_modules/express/lib/response.js:767:10)
at ServerResponse.json (/worker/node_modules/express/lib/response.js:264:10)
at ServerResponse.send (/worker/node_modules/express/lib/response.js:158:21)
at exports.uploadImage.functions.https.onRequest (/srv/index.js:21:13)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
And on the web app I get a 500 error response without any data returned.
This is how my request is made:
const data = {
image: 'image url here',
options: {
tags: 'document,doctor',
}
};
axios.post('*** FUNCTION URL HERE***/uploadImage', data, {
headers: {
'Content-Type': 'application/json',
},
})
However if I make the request through Postman then I get a 200 response with the data I expect.
This is what my functions/index.js file look like:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cloudinary = require('cloudinary');
const cors = require('cors')({origin: true});
admin.initializeApp();
exports.uploadImage = functions.https.onRequest(async (req, res) => {
cors(req, res, () => {});
cloudinary.config({
cloud_name: 'config here',
api_key: 'config here',
api_secret: 'config here',
});
try {
const data = Object.apply({folder: 'myortho'}, req.body.options);
const response = await cloudinary.uploader.upload(req.body.image, data);
return res.send(response);
}catch(error) {
return res.send(500, {message: 'Error uploading image', error});
}
});
I saw many questions relating to the error I am getting but many of those questions are referring to people's routing issues (which isn't a concern for me since this is a Cloud Function and not my own Node.js back-end server).
It is also an error I cannot understand since it is successful through Postman but not through my browser so I am truly confused.