首页 > 解决方案 > Cloud Firestore 触发器不适用于 Cloud 功能

问题描述

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

触发器类型: Cloud Firestore (Beta) 事件类型: 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);
    });
  
  
  });

控制台片段: 在此处输入图像描述

标签: javascriptnode.jsfirebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


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

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

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


推荐阅读