【问题标题】:Error trying to set auto back up Firestore, cloud function尝试设置自动备份 Firestore、云功能时出错
【发布时间】:2020-11-01 07:08:03
【问题描述】:

我在这里学习本教程:Tutorial

一切似乎都很好,它允许我在教程中做所有事情,但是当我运行该函数时,我得到了这个错误。

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.scheduledFirestoreExport (/workspace/index.js:13: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)
insertId: "000000-8410c5c7-8304-42b6-b2b6-dd55a54e8cab"
resource: {2}
timestamp: "2020-07-11T18:14:35.981Z"
severity: "ERROR"
labels: {1}
logName: "projects/b-b-b-app/logs/cloudfunctions.googleapis.com%2Fcloud-functions"
trace: "projects/b-b-b-app/traces/d7c07a715d0106225d9963ce2a046489"
receiveTimestamp: "2020-07-11T18:14:44.813410062Z"
}

我看不出可能是什么问题。 我按照教程中的要求更改了存储桶和应用程序 ID。

我正在使用 Blaze 计划,可以使用 shell 命令和使用手动将数据库导出到存储桶中

gcloud firestore export gs://bbbdata-backup

我正在使用 Firebase 站点上的 GCP 控制台并使用此代码。

const firestore = require('@google-cloud/firestore');
const client = new firestore.v1.FirestoreAdminClient();

const bucket = 'gs://bbbdata-backup'

exports.scheduledFirestoreExport = (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: [],
})
.then(responses => {
  const response = responses[0];
  console.log(`Operation Name: ${response['name']}`);
  return response;
})
.catch(err => {
  console.error(err);
});
};

【问题讨论】:

  • 请编辑您的问题以包含任何人都可以重现该问题的最小精确代码。指向文档(或其他场外资源)的链接还不够好。确切的再现应该在问题本身中。它通常还有助于了解您的项目是采用付费计划还是 Firebase 的免费计划。
  • 为弗兰克的提醒干杯我已经改变了它

标签: node.js firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

按照 OP 引用的教程,我遇到了完全相同的错误。使用的运行时:Node.js 14

问题的根本原因:process.env.GCLOUD_PROJECT 的值未定义。

解决方法:转到 GCP 控制台 -> 主页。请注意您的Project ID。将 process.env.GCLOUD_PROJECT 替换为“Project ID”字符串。 Cloud Function 将按预期工作

注意:GCLOUD_PROJECT 环境变量在 Node.js 10 运行时中丢失似乎是一个已知问题。这个错误报告包含很多额外的指针:https://github.com/firebase/firebase-functions/issues/437

【讨论】:

    【解决方案2】:

    我去年也遇到过类似的问题,可能是你缺少一些权限,我会这样做,希望这对你有用:

    import * as functions from 'firebase-functions'
    import { auth } from 'google-auth-library'
    
    export const generateBackup = async () => {
      const client = await auth.getClient({
        scopes: [
          'https://www.googleapis.com/auth/datastore',
          'https://www.googleapis.com/auth/cloud-platform'
        ]
      })
    
      const path = `YOUR_FOLDER_NAME_FOR_THE_BACKUP`
      const BUCKET_NAME = `YOUR_BUCKET_NAME_HERE`
    
      const projectId = await auth.getProjectId()
      const url = `https://firestore.googleapis.com/v1beta1/projects/${projectId}/databases/(default):exportDocuments`
      const backup_route = `gs://${BUCKET_NAME}/${path}`
    
      return client.request({
        url,
        method: 'POST',
        data: {
            outputUriPrefix: backup_route,
            // collectionsIds: [] // if you want to specify which collections to export, none means all
        }
      })
      .catch(async (e) => {
        return Promise.reject({ message: e.message })
      })
    
    }
    
    

    然后,您可以决定这是您对该函数的触发器并相应地执行它。

    注意:进入项目的IAM部分,找到App Engine服务账号,需要添加角色Cloud Datastore Import Export Admin,否则会失败。

    你可以阅读更多关于它here它非常详细。

    干杯。

    【讨论】:

      猜你喜欢
      • 2019-12-21
      • 2021-09-25
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      相关资源
      最近更新 更多