Firebase Functions无法连接Google Cloud SQL,执行请求超时无返回
Key Fixes & Debugging Steps
Fix Parameterized Query (Eliminate Injection Risk & Silent Failures)
Your current string interpolation can cause syntax errors (e.g., special characters inuserAuthUid) that trigger unreported timeouts. Switch to parameterized queries:const mssql = require('mssql'); const settings = require('../settings.js'); const pool = await mssql.connect(settings.sqlConfig); const queryRes = await pool.request() .input('userAuthUid', mssql.NVarChar, userAuthUid) .input('value', mssql.Int /* Match your SP's parameter type */, value) .query('EXEC spUserDelete @userAuthUid, @value');Adjust Firebase Function Timeout Limits
Default timeouts (60s for HTTP functions, 9 mins for background functions) can be hit if your stored procedure runs long. Updatefirebase.json:{ "functions": { "runtimeOptions": { "timeoutSeconds": 300 // Max allowed for your function type } } }Validate Cloud SQL Connection Health
- Check the Cloud SQL console for instance outages, maintenance events, or resource constraints.
- Confirm the Firebase Function's service account has the
Cloud SQL ClientIAM role. - Verify firewall rules/VPC settings haven't changed to block traffic between Functions and SQL.
Add Error Logging to Catch Hidden Issues
Missing error handling hides critical details. Wrap your code in try/catch:try { const pool = await mssql.connect(settings.sqlConfig); const queryRes = await pool.request() .input('userAuthUid', mssql.NVarChar, userAuthUid) .input('value', mssql.Int, value) .query('EXEC spUserDelete @userAuthUid, @value'); console.log('Query completed:', queryRes); return queryRes; } catch (err) { console.error('Full error:', err); throw new Error(`DB operation failed: ${err.message}`); } finally { await mssql.close(); }Check for Stored Procedure Hangs
RunspUserDeletedirectly in your SQL tool with the same parameters to confirm it completes. Look for deadlocks, long-running queries, or locked resources in the SQL Server instance.
内容的提问来源于stack exchange,提问作者Abdelrahman

