Fixing 'Cannot read property charCodeAt of undefined' in Google Cloud Functions

User avatar

Colin Armstrong


After deploying a new function on Google Cloud Functions, when attempting to run it you may encounter a stack trace like so:

1 2 3 4 5 6 7 8 9 10 Function execution took 11 ms, finished with status: 'error'  at processTicksAndRejections (internal/process/task_queues.js:95:5)  at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:199:28  at Array.forEach (<anonymous>)  at exports.scheduledFirestoreExport (/workspace/index.js:7:31)  at FirestoreAdminClient.databasePath (/workspace/node_modules/@google-cloud/firestore/build/src/v1/firestore_admin_client.js:904:57)  at /workspace/node_modules/google-gax/build/src/pathTemplate.js:120:29 TypeError: Cannot read property 'charCodeAt' of undefined at peg$parsetemplate (/workspace/node_modules/google-gax/build/src/pathTemplateParser.js:304:17) at Object.peg$parse [as parse] (/workspace/node_modules/google-gax/build/src/pathTemplateParser.js:633:18) at new PathTemplate (/workspace/node_modules/google-gax/build/src/pathTemplate.js:55:54)  at PathTemplate.render (/workspace/node_modules/google-gax/build/src/pathTemplate.js:114:23)

This isn’t the most descriptive stack trace, but it means that the first parameter in the following method call is undefined:

1 2 3 4 const databaseName = client.databasePath( process.env.GCLOUD_PROJECT, '(default)' );

process.env.GCLOUD_PROJECT is undefined. But why?

Well, if you’re running a NodeJS runtime of 10 or above (which, if your Cloud Function is using NodeJS, you almost certainly are!) Google Cloud removed this as an environment variable.

So, to fix this, just use your actual project ID when constructing the databasePath:

1 2 3 4 const databaseName = client.databasePath( 'my-nifty-googlecloud-project-id', '(default)' );

And voila! Problem solved.

Credit to this GitHub thread for clearing things up.

  • Loading comments...

Paragraph is on Paragraph, a web3 publishing & newsletter platform.


News, features, and interesting tidbits about Paragraph (and beyond).