CodexBloom - Programming Q&A Platform

GCP Cloud Functions timing out when accessing Firestore with Node.js and CORS issues

πŸ‘€ Views: 80 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-09
google-cloud-functions firestore cors node.js JavaScript

Hey everyone, I'm running into an issue that's driving me crazy... I'm experiencing a timeout scenario with my GCP Cloud Function when trying to access a Firestore document. The function is supposed to retrieve user data based on an ID passed in the request, but it's consistently timing out after 60 seconds, even though the document exists and I can access it directly from the Firestore console. Here's the relevant part of my code: ```javascript const admin = require('firebase-admin'); admin.initializeApp(); exports.getUserData = async (req, res) => { const userId = req.query.id; try { const userDoc = await admin.firestore().collection('users').doc(userId).get(); if (!userDoc.exists) { return res.status(404).send('User not found'); } return res.status(200).send(userDoc.data()); } catch (behavior) { console.behavior('behavior fetching user data:', behavior); return res.status(500).send('Internal Server behavior'); } }; ``` I've also set up CORS like this: ```javascript const cors = require('cors'); app.use(cors({ origin: true })); ``` I've tried increasing the timeout setting for the function, but it doesn’t seem to help. I also checked the Firestore rules and they seem correctly set for read access. I'm not seeing any specific behavior messages in the logs that indicate what might be going wrong. The execution log shows the function is invoked, but it just hangs until it times out. Is there something I'm overlooking in this configuration that could be causing the timeout? Any insights would be appreciated! What am I doing wrong?