简体   繁体   English

在嵌套集合中将 onCreate 与 Firebase 云函数一起使用

[英]Using onCreate with Firebase Cloud Functions in Nested collection

I am looking to create a Firebase Cloud Function that gets called every time a document gets created.我希望创建一个 Firebase Cloud Function,每次创建文档时都会调用它。 Using their tutorial I have the following code in:使用他们的教程,我有以下代码:

exports.sendConfirmationEmail = functions.firestore.document('collection1/{resId}')
    .onCreate((snap, context) => {
      // my method
    });

now this method gets called every time a document gets added to collection1.现在每次将文档添加到 collection1 时都会调用此方法。 But I have a database structure that looks like the following:但是我有一个如下所示的数据库结构:

| | businesses企业

| | => business document => 商业文件

| | => | => | reservations预订

Not sure if that makes any sense but essentially there is a business collection where each business document contains a reservations subcollection.不确定这是否有意义,但本质上有一个业务集合,其中每个业务文档都包含一个预订子集合。 I need to write a function that gets executed every time a document gets added to the reservations collection in each business, but still be able to access the business document somehow.我需要编写一个 function ,每次将文档添加到每个业务的预订集合时都会执行它,但仍然能够以某种方式访问业务文档。 How can I accomplish this?我怎样才能做到这一点? Thanks for any help.谢谢你的帮助。

Your function definition will look like this:您的 function 定义将如下所示:

exports.yourFunctionName =
functions.firestore.document('businesses/{businessId}/reservations/{reservationId}')
.onCreate((snap, context) => {
    // your code goes here
});

For any one else, looking here is what this would look like when trying to retrieve both the business and the reservation:对于其他任何人,查看这里是尝试检索业务和预订时的样子:

exports.getReservationFromBusiness = functions.firestore.document('businesses/{bisId}/reservations/{resId}')
    .onCreate((snap, context) => {
      const businessId = context.params.bisId;
      const reservation = snap.data();
    });

This code is from the documentation and Github page .此代码来自文档和 Github 页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM