i am trying to create an express - api with firestore database. But only thing i got is CORS errors. Here is the API code ; `
const functions = require("firebase-functions");
const admin = require("firebase-admin")
var serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const express = require("express")
const cors = require("cors")
//Main app
const app = express()
app.use(cors({origin:true}));
//Main database reference
const db = admin.firestore()
//Routes
app.get('/', (req,res) =>{
return res.status(200).send('Hello, world!')
})
//Create => post()
app.post("/api/create", (req,res) => {
(async () => {
try{
await db.collection('userDetails').doc(`/${Date.now()}/`).create({
id: Date.now(),
name: req.body.name,
mobile:req.body.mobile,
address: req.body.address
})
return res.status(200).send({status:'Success', msg: 'Data Saved'})
}catch(err){
}
})()
})
exports.app = functions.https.onRequest(app);
`
And the error is : Access to XMLHttpRequest at 'myApi' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Thanks already, have a great day/night !
I'm trying to get the data from api, but im getting CORS error. Network tab error image
Well, i'm kinda dumb i guess. It wasn't the backend code but frontend. I had typo when im doing the api call. Backend code is working fine.