【问题标题】:Cloud Firestore trigger not working for Cloud functionCloud Firestore 触发器不适用于 Cloud 功能
【发布时间】:2020-10-15 16:13:56
【问题描述】:

我正在尝试使用 Cloud Firestore 触发器调用 Cloud Function。基本上,每当将新文档添加到“评论”时,我的云功能都会完全导出我的子集合“评论”。我的“评论”子集合中有大约 6-7 个文档。我通过控制台部署了功能,部署完成。但是,每当我将新文档添加到“评论”子集合中时,都不会触发此功能。谁能告诉我可能是什么问题?

触发器类型: Cloud Firestore(测试版) 事件类型: providers/cloud.firestore/eventTypes/document.write 文档路径: test_restaurant/{reviews}

编辑: 我希望我的云功能仅将添加到我的 Firestore 的新文档导出到我的 GCP 存储桶中。以下是我正在尝试编写的函数:

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

const bucket = 'gs://ABC/trigger_test'
exports.newFirestoreBackup = functions.firestore
  .document('test_restaurant/{Id}/reviews/{Id}') 
  .onWrite((change, context) => {
   const databaseName = client.databasePath(
   // process.env.GCLOUD_PROJECT,
   "FS-123",
    '(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: ['reviews'],
    })
    .then(responses => {
      const response = responses[0];
      console.log(`Operation Name: ${response['name']}`);
      return response;
    })
    .catch(err => {
      console.error(err);
    });
  
  
  });

控制台片段:

【问题讨论】:

  • 你能分享一下你的云函数的整个代码吗?
  • 用我的云函数代码@Renaud Tarnec 编辑了问题

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


【解决方案1】:

您不共享任何代码,但如果您希望“每当[您]将新文档添加到reviews 子集合”时触发您的云函数,则应使用以下路径声明它:

exports.createUser = functions.firestore
    .document('test_restaurant/{restaurantId}/reviews/{reviewId}')
    .onCreate((snap, context) => {...});

您也可以使用onWrite() 触发器,路径相同。

【讨论】:

  • 即使我通过控制台创建云功能,是否必须在我的 index.js 中包含上述代码?我已经在控制台本身中指定了我的触发器和文档路径,所以我需要在我的云函数中再次提及这一点吗? (我也附上了控制台sn-p)
猜你喜欢
  • 2020-11-09
  • 2018-03-27
  • 1970-01-01
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 2020-02-04
相关资源
最近更新 更多