Thanks to the great support by Teju Nareddy, to get ESPv2 Beta working, Now I have been working for 5 hours to get access from my React app to the API.
that works with my React app would work with the cloud function. So I configured a bay pass of CORS in the open API conf file and wrapped the cloud function in a CORS function. But I still get the same error:
(anonymous) @ VM933:1
(anonymous) @ Service.js:25
c @ runtime.js:45
(anonymous) @ runtime.js:264
e.<computed> @ runtime.js:98
r @ asyncToGenerator.js:3
s @ asyncToGenerator.js:25
Promise.then (async)
r @ asyncToGenerator.js:13
s @ asyncToGenerator.js:25
(anonymous) @ asyncToGenerator.js:32
(anonymous) @ asyncToGenerator.js:21
(anonymous) @ Service.js:43
(anonymous) @ react-dom.production.min.js:5800
t.unstable_runWithPriority @ scheduler.production.min.js:338
ua @ react-dom.production.min.js:2513
Hs @ react-dom.production.min.js:5547
Ms @ react-dom.production.min.js:4817
(anonymous) @ react-dom.production.min.js:2543
t.unstable_runWithPriority @ scheduler.production.min.js:338
ua @ react-dom.production.min.js:2513
ma @ react-dom.production.min.js:2538
pa @ react-dom.production.min.js:2528
ks @ react-dom.production.min.js:4865
(anonymous) @ react-dom.production.min.js:1783
Service.js:43 TypeError: Failed to fetch
This is How I wrap it in my cloud function (pseudo-code):
var corsOptions = {
methods: ['POST', 'GET', 'DELETE', 'PUT'],
credentials: true,
maxAge: 3600,
preflightContinue: true,
optionsSuccessStatus: 200
};
const cors = require('cors')(corsOptions);
exports.sample = functions.https.onRequest((req, res) => {
cors(req, res, () => {
HERE IS MY DAMN CODE!
});
});
But the React app can't read google cloud functions. But the app can read the
render.com API function. Both have almost the same CORS config.
what is the best approach to solving it, without wasting all my resources on it?
I also tried to set it in the ESPv2 but that only coursed deploy errors, so I moved it to the function instead wear I thought that I could solve it by using the same code as in my other API servers.
In my node.js express app, I have one difference from the cloud function, and I don't know if it's important:
Sometimes to make it work you need to add the options before the request. How you do that when you wrap it, I have no clue..
app.options('/update', checkJwt, cors(corsOptions));
app.post('/update', checkJwt, cors(corsOptions), function (req, res) {
An even more frustrated developer...