0
votes

I am trying to set up a function in Cloud Firestore that will trigger whenever a new document is added to a particular collection. I wish to do it using TypeScript as I'm told its easier to work with when writing asynchronous code. The following JavaScript code works as expected and triggers whenever I add a new document at the specified location:

//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:

//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. Thanks for any help you can give me.

1
functions.firestore != functions.database. Different database products.Doug Stevenson

1 Answers

2
votes

In your javascript example you faciliate the Firestore Database functions.firestore .document. However your TypeScript example is using the Realtime Database 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 })