【问题标题】:Cloud function fails after changing Runtime to Node.js 10将 Runtime 更改为 Node.js 10 后云功能失败
【发布时间】:2020-09-14 01:41:42
【问题描述】:

我正在尝试通过云功能进行 Firebase 备份。当我使用 Runtime: Node.js 8 时,该函数运行得非常好。但是,由于它很快就会被弃用,我现在必须使用 Node.js 10。我的 fcloud 函数现在失败并出现以下错误:

错误:函数执行失败。细节: 无法读取未定义的属性“charCodeAt”

详细的错误日志:

2020-05-27 11:01:21.820 IST
firestore_export
8kxlp9s867dy
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 segments.forEach.segment (/workspace/node_modules/google-gax/build/src/pathTemplate.js:120:29) at Array.forEach (<anonymous>) at PathTemplate.render (/workspace/node_modules/google-gax/build/src/pathTemplate.js:114:23) at FirestoreAdminClient.databasePath (/workspace/node_modules/@google-cloud/firestore/build/src/v1/firestore_admin_client.js:904:57) at exports.scheduledFirestoreBackup (/workspace/index.js:6:31) at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:330:28) at process._tickCallback (internal/process/next_tick.js:68:7)
Expand all | Collapse all
{
 insertId: "000000-f688386c-8f2b-4146-aaf7-1fe67c656fa2"  

labels: {…}  
 logName: "projects/firestore-249705/logs/cloudfunctions.googleapis.com%2Fcloud-functions"  
 receiveTimestamp: "2020-05-27T05:31:31.171084310Z"  

resource: {…}  
 severity: "ERROR"  
 textPayload: "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 segments.forEach.segment (/workspace/node_modules/google-gax/build/src/pathTemplate.js:120:29)
    at Array.forEach (<anonymous>)
    at PathTemplate.render (/workspace/node_modules/google-gax/build/src/pathTemplate.js:114:23)
    at FirestoreAdminClient.databasePath (/workspace/node_modules/@google-cloud/firestore/build/src/v1/firestore_admin_client.js:904:57)
    at exports.scheduledFirestoreBackup (/workspace/index.js:6:31)
    at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:330:28)
    at process._tickCallback (internal/process/next_tick.js:68:7)"  
 timestamp: "2020-05-27T05:31:21.820Z"  
 trace: "projects/firestore-249705/traces/74e27700d135763bc4e7892ebb1a2333"  
}

我的 index.js 如下:

const firestore = require('@google-cloud/firestore');
const client = new firestore.v1.FirestoreAdminClient();
// Replace BUCKET_NAME
const bucket = 'gs://gcp_firestore_ae2/firestore_export'
exports.scheduledFirestoreBackup = (event, context) => {
  const databaseName = client.databasePath(
    process.env.GCLOUD_PROJECT,
    '(default)'
  );
return client
    .exportDocuments({
      name: databaseName,
      outputUriPrefix: bucket,
      // Leave collectionIds empty to export all collections
      // or define a list of collection IDs:
      // collectionIds: ['users', 'posts']
      collectionIds: ['most_valuable_items','nric_img','pay_slip','pic_of_ofc_entrance'],
    })
    .then(responses => {
      const response = responses[0];
      console.log(`Operation Name: ${response['name']}`);
      return response;
    })
    .catch(err => {
      console.error(err);
    });
};

【问题讨论】:

  • 请编辑问题以显示完整的错误,并帮助我们确定此处引用的代码行。
  • 添加了详细的错误日志@Doug Stevenson
  • @google-cloud/firestore - 使用什么版本?
  • 8.3.0 是@Sergey Reus 的版本
  • 可以this github issue 相关吗?

标签: javascript firebase google-cloud-platform google-cloud-firestore google-cloud-functions


【解决方案1】:

我遇到了同样的问题,在以前的版本中,我们似乎不需要在环境变量中 GCLOUD_PROJECT 即它会自动检测它,但是从节点 10 开始,我们需要明确地传递它。 我就是这样解决的。 因此,与其硬编码,不如尝试在云函数环境变量中传递 GCLOUD_PROJECT

注意:GCLOUD_PROJECT 是项目 ID,而不是项目名称。

【讨论】:

    【解决方案2】:

    我有同样的问题。我通过更改来修复它:

    process.env.GCLOUD_PROJECT

    到我的实际项目 ID(例如“my_app_37274”)

    【讨论】:

    • 谢谢你
    猜你喜欢
    • 2020-10-19
    • 1970-01-01
    • 2021-08-09
    • 2022-09-01
    • 2021-05-31
    • 2019-01-30
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多