尝试设置自动备份Firestore,云功能时出错

时间:2020-07-11 18:20:51

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

我在这里关注本教程:Tutorial

一切似乎都可以,它使我可以完成教程中的所有操作,但是当我运行该函数时,会出现此错误。

Start()
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)

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

我处于Blaze计划,可以使用shell命令并使用

将数据库手动导出到存储桶中

gcloud firestore导出gs:// bbbdata-backup

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

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"
}

1 个答案:

答案 0 :(得分:0)

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

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,它非常详细。

干杯。