【问题标题】:Cannot read property 'toString' of undefined when firestore would run backups当 Firestore 运行备份时,无法读取未定义的属性“toString”
【发布时间】:2020-09-14 16:15:48
【问题描述】:

我想通过每天安排一次来处理“导出 Firestore 数据”。这是firebase official document。我试过但它不起作用,因为下面的错误:

无法读取未定义的属性“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')
      })
  })

【问题讨论】:

  • 您在控制台中记录响应时是否收到此错误?你能分享一下你得到的回应吗?发生这种情况可能是因为响应未定义。您能否验证一下 .env.GCP_PROJECT 不为空?

标签: firebase google-cloud-firestore


【解决方案1】:

如果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)',
    )

【讨论】:

  • 哇...花了一整天的时间在网上查看我的代码和答案...这为我解决了问题。
猜你喜欢
  • 1970-01-01
  • 2021-04-09
  • 2020-08-01
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多