我希望创建一个Firebase Cloud Function,该功能在每次创建文档时都会被调用。使用他们的教程,我在以下代码中:
exports.sendConfirmationEmail = functions.firestore.document('collection1/{resId}')
.onCreate((snap, context) => {
// my method
});
现在,每次将文档添加到collection1时都会调用此方法。但是我有一个数据库结构,如下所示:
|商家
| =>商业文件
| => |预订
[不确定是否有意义,但本质上是存在一个业务集合,其中每个业务文档都包含一个预订子集合。我需要编写一个函数,每次将文档添加到每个业务的保留集合中时都会执行该函数,但是仍然能够以某种方式访问该业务文档。我该怎么做?感谢您的帮助。
您的函数定义将如下所示:
exports.yourFunctionName =
functions.firestore.document('businesses/{businessId}/reservations/{reservationId}')
.onCreate((snap, context) => {
// your code goes here
});
对于其他任何人,这里都是试图同时检索业务和预订时的外观:
exports.getReservationFromBusiness = functions.firestore.document('businesses/{bisId}/reservations/{resId}')
.onCreate((snap, context) => {
const businessId = context.params.bisId;
const reservation = snap.data();
});
此代码来自documentation和Github page。