当 firestore 运行备份时,无法读取未定义的属性“toString”

k10*_*10a 1 firebase google-cloud-firestore

我想通过每天安排一次来“导出 Firestore 数据”。这是firebase 官方文档。我尝试过,但它不起作用,因为出现以下错误:

无法读取未定义的属性“toString”

代码:

const functions = require('firebase-functions')
const firestoreClient = require('@google-cloud/firestore')
const admin = require('firebase-admin')
admin.initializeApp()
const firestore = admin.firestore()

//------------------------------------------------------
// backup
//------------------------------------------------------
const client = new firestoreClient.v1.FirestoreAdminClient()
exports.scheduledFirestoreExport = functions.pubsub
  .schedule('every 24 hours')
  .onRun(() => {
    const databaseName = client.databasePath(
      process.env.GCP_PROJECT,
      '(default)',
    )
    const bucket = 'gs://backups-firestore'
    return client
      .exportDocuments({
        name: databaseName,
        outputUriPrefix: bucket,
        collectionIds: [
          'users',
        ],
      })
      .then((responses) => {
        const response = responses[0]
        console.log(`Operation Name: ${response['name']}`)
        return response
      })
      .catch((err) => {
        console.error(err)
        throw new Error('Export operation failed')
      })
  })
Run Code Online (Sandbox Code Playgroud)

小智 5

process.env.GCP_PROJECT如果未定义, 您将收到此错误消息。process.env.GCP_PROJECT在 Node.js 10 中未定义,而它在 Node.js 8 中工作。

如果你这样做,它应该有效:

const projectId = process.env.GCP_PROJECT || process.env.GCLOUD_PROJECT;
const databaseName = client.databasePath(
      projectId,
      '(default)',
    )
Run Code Online (Sandbox Code Playgroud)