I have simple code for functions as given below
var functions = require('firebase-functions');
const cors = require('cors')({
origin: true,
});
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
exports.helloWorld = functions.https.onRequest(function (request, response) {
var carF = cors();
carF((request, response, () => {
response.status(200).send("Hello from Firebase suraj!");
}));
});
but while trying to access via firebase serve it gives following error
{
"stack": "TypeError: Cannot read property 'headers' of undefined\n at /home/suraj/Documents/Eatr/firebase/node_modules/cors/lib/index.js:219:32\n at optionsCallback (/home/suraj/Documents/Eatr/firebase/node_modules/cors/lib/index.js:199:9)\n at corsMiddleware (/home/suraj/Documents/Eatr/firebase/node_modules/cors/lib/index.js:204:7)\n at /home/suraj/Documents/Eatr/firebase/functions/index.js:8:16\n at cloudFunction (/home/suraj/Documents/Eatr/firebase/functions/node_modules/firebase-functions/lib/providers/https.js:26:41)\n at app.use (/home/suraj/.npm-global/lib/node_modules/firebase-tools/node_modules/@google-cloud/functions-emulator/src/supervisor/worker.js:130:11)\n at Layer.handle [as handle_request] (/home/suraj/.npm-global/lib/node_modules/firebase-tools/node_modules/express/lib/router/layer.js:95:5)\n at trim_prefix (/home/suraj/.npm-global/lib/node_modules/firebase-tools/node_modules/express/lib/router/index.js:317:13)\n at /home/suraj/.npm-global/lib/node_modules/firebase-tools/node_modules/express/lib/router/index.js:284:7\n at Function.process_params (/home/suraj/.npm-global/lib/node_modules/firebase-tools/node_modules/express/lib/router/index.js:335:12)",
"message": "Cannot read property 'headers' of undefined",
"name": "TypeError"
}
We have some samples showing how to use CORS: https://github.com/firebase/functions-samples/blob/master/quickstarts/time-server/functions/index.js
Looks like you should try this instead:
var functions = require('firebase-functions');
const cors = require('cors')({
origin: true,
});
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.helloWorld = functions.https.onRequest(function (request, response) {
cors(request, response, () => {
response.status(200).send("Hello from Firebase suraj!");
});
});
I'm having this issue, except it's the
({
origin: true,
})
that is causing the error. As soon as I remove it, the function works.
Most helpful comment
I'm having this issue, except it's the
that is causing the error. As soon as I remove it, the function works.