简体   繁体   English

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

[英]Can't get onCreate function to trigger using TypeScript on Cloud Firestore

I am trying to set up a function in Cloud Firestore that will trigger whenever a new document is added to a particular collection.我正在尝试在 Cloud Firestore 中设置一个函数,该函数将在新文档添加到特定集合时触发。 I wish to do it using TypeScript as I'm told its easier to work with when writing asynchronous code.我希望使用 TypeScript 来完成它,因为有人告诉我它在编写异步代码时更容易使用。 The following JavaScript code works as expected and triggers whenever I add a new document at the specified location:以下 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 })        

      });

But then when I try to do the same thing using TypeScript, it fails to trigger - nothing at all happens when I add the document:但是当我尝试使用 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 })
})

I'm very new to Firestore functions, and have no clue at all what I'm doing wrong.我对 Firestore 功能很陌生,完全不知道我做错了什么。 Thanks for any help you can give me.感谢你给与我的帮助。

In your javascript example you faciliate the Firestore Database functions.firestore .document .在您的 javascript 示例中,您functions.firestore .document了 Firestore 数据库functions.firestore .document However your TypeScript example is using the Realtime Database functions.database.ref .但是,您的 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 })

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

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