【问题标题】:Can't get onCreate function to trigger using TypeScript on Cloud Firestore无法使用 Cloud Firestore 上的 TypeScript 触发 onCreate 函数
【发布时间】:2020-04-29 06:01:14
【问题描述】:

我正在尝试在 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 功能非常陌生,完全不知道我做错了什么。感谢您能给我的任何帮助。

【问题讨论】:

  • functions.firestore != functions.database。不同的数据库产品。

标签: javascript typescript firebase google-cloud-firestore


【解决方案1】:

在您的 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 })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 2022-08-16
    • 2019-10-12
    • 2017-12-13
    • 1970-01-01
    相关资源
    最近更新 更多