I'm getting the following error on the browser when I run my react app on localhost which sends a payload to a Google Firebase function:
Access to fetch at 'https://< myproject >.cloudfunctions.net/< myfunction >' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
My Google Firebase function has the following code:
exports.<myfunction> = functions.https.onRequest(async (req, res) => {
res.set('Access-Control-Allow-Origin', 'http://localhost:3000');
res.set('Access-Control-Allow-Headers', '*');
console.log(req.body);
res.end()
});
The error I get on my firebase functions console log is the following:
SyntaxError: Unexpected token # in JSON at position 0 at JSON.parse () at createStrictSyntaxError (/worker/node_modules/body-parser/lib/types/json.js:157:10) at parse (/worker/node_modules/body-parser/lib/types/json.js:83:15) at /worker/node_modules/body-parser/lib/read.js:121:18 at invokeCallback (/worker/node_modules/raw-body/index.js:224:16) at done (/worker/node_modules/raw-body/index.js:213:7) at IncomingMessage.onEnd (/worker/node_modules/raw-body/index.js:273:7) at emitNone (events.js:111:20) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1064:12)
My Client side code which triggers the function is the following:
submit(name) {
fetch('https://< myproject >.cloudfunctions.net/< my function >', {method: 'POST', headers: {"Content-Type": "application/json",
},
body: 'mytoken'
}).then(result => {console.log(result)});
}
Also, everything was working fine just minutes and days earlier! It is as though this error started popping up out of nowhere.