In my react app I'm sending a post request with 2 attributes to a cloud function name and phone but returns a restriction from CORS in the react app, I am using cloud functions with Typescript This is my code from my react app
axios
.post(
"https://us-central1-react-firebase-athentica-64987.cloudfunctions.net/firestoreTtlCallback",
{
name: `${this.state.contactName}`,
phone: `${this.state.phone}`,
}
)
.then(function(response) {
// eslint-disable-next-line no-console
console.log(response);
})
.catch(function(error) {
// eslint-disable-next-line no-console
console.log(error);
});
}); ```
And this is from my cloud function:
exports.firestoreTtlCallback = functions.https.onRequest(async(req , res) => {
res.set('Access-Control-Allow-Origin', "*")
res.set('Access-Control-Allow-Methods', 'GET, POST')
res.set('Access-Control-Allow-Headers', 'Content-Type');
const name = req.body.name;
console.log(name)
//recibir nombre de contacto y numero de telefono
const payload = req.body as ExpirationTaskPayload
try {
// buscar usuario y obtener token
const user = await db.collection('users').doc(payload.uid).get()
const data:any = user.data()
const counter = await admin.firestore().collection("users").doc(payload.uid).collection("kards").doc("0").collection("kardsRef").get();
const doc = await admin.firestore().doc(`/users/${payload.uid}/kards/0/kardsRef/${counter.size}`)
doc.set({ msg: "hola" })
var message = {
data: {
score: '850',
time: '2:45'
},
token: data.token
};
//enviar notificacion
await admin.messaging().send(message)
// Response is a message ID string.
// borrar notificacion firestore
await admin.firestore().doc(payload.docPath).delete()
return res.status(200).send(`Task has been executed`);
}
catch (error) {
console.error(error)
return res.status(500).send(error)
}
});
this is the error I get:
Access to fetch at 'https://us-central1-react-firebase-athentica-64987.cloudfunctions.net/firestoreTtlCallback' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
https://us-central1-react-firebase-athentica-64987.cloudfunctions.net/firestoreTtlCallbackendpoint isn’t configured to respond to OPTIONS requests in the way that’s necessary for CORS to work. That endpoint must be configured to respond to unauthenticated OPTIONS requests with a 200 OK success response. Right now it’s apparently responding with some other HTTP status code: maybe a 405 error or some other 4xx error. - sideshowbarkerhttps://us-central1-react-firebase-athentica-64987.cloudfunctions.net/firestoreTtlCallbackendpoint, and see what messages are getting logged there before the server responds with that 500. But given the code shown in the question, it looks like it might be your own code that’s throwing the 500 — because the code in the try block fails. - sideshowbarker