无法获得onCreate函数以在Cloud Firestore上使用TypeScript触发

时间:2020-01-12 05:11:25

标签: javascript typescript firebase google-cloud-firestore

我正在尝试在Cloud Firestore中设置一个功能,该功能将在将新文档添加到特定集合时触发。我希望使用TypeScript做到这一点,因为在编写异步代码时,我被告知它更易于使用。以下JavaScript代码会按预期运行,并在我在指定位置添加新文档时触发:

//This JavaScript code works
import * as functions from 'firebase-functions';

exports.waitReportCreatedJS = functions.firestore
    .document('merchant_locations/{merchantId}/wait_period/{waitId}')
    .onCreate((snapshot, context) => {
        console.log('WaitReportCreated has been triggered');
        const merchantId = context.params.merchantId
        const waitId = context.params.waitId
        console.log(`New wait recorded: ${waitId} at ${merchantId}`);
        return snapshot.ref.update({ waitId: waitId })        

      });

但是,当我尝试使用TypeScript执行相同操作时,它无法触发-添加文档时什么也没发生:

//But this TypeScript code doesn't seem to even trigger
import * as functions from 'firebase-functions';

export const waitReportCreatedTS = functions.database
.ref('merchant_locations/{merchantId}/wait_period/{waitId}')
.onCreate((snapshot, context) => {
    console.log('WaitReportCreated has been triggered');
    const merchantId = context.params.merchantId
    const waitId = context.params.waitId
    console.log(`New wait recorded: ${waitId} at ${merchantId}`);
    return snapshot.ref.update({ waitId: waitId })
})

我对Firestore功能非常陌生,完全不知道我做错了什么。谢谢您能给我的帮助。

1 个答案:

答案 0 :(得分:2)

在您的JavaScript示例中,您使用了Firestore数据库functions.firestore .document。但是,您的TypeScript示例使用的是实时数据库functions.database.ref

import * as functions from 'firebase-functions';

export const waitReportCreatedTS = functions.firestore
    .document('merchant_locations/{merchantId}/wait_period/{waitId}')
    .onCreate((snapshot, context) => {
    console.log('WaitReportCreated has been triggered');
    const merchantId = context.params.merchantId
    const waitId = context.params.waitId
    console.log(`New wait recorded: ${waitId} at ${merchantId}`);
    return snapshot.ref.update({ waitId: waitId })