Struggling to activate the onCreate function in TypeScript when using Cloud Firestore

Currently, I am attempting to establish a function within Cloud Firestore that will activate whenever a new document is inserted into a specific collection. My intention is to achieve this using TypeScript because it supposedly simplifies working with asynchronous code. The provided JavaScript code functions as anticipated and triggers whenever a new document is added at the designated location:

//The JavaScript snippet below successfully activates
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 });        

      });

However, when I attempt to replicate the same behavior using TypeScript, it fails to trigger - no action occurs when adding the document:

//On the other hand, this TypeScript code does not appear to trigger at all
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 });
})

Given my lack of experience in Firestore functions, I am completely clueless as to what I might be doing incorrectly. Any assistance or guidance you could provide would be greatly appreciated. Thank you.

Answer №1

Your illustration in Javascript involves the Firestore Database using

functions.firestore
    .document
. However, in your TypeScript example, you are utilizing the Realtime Database with 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 })

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Please dismiss the pop-up and reload the main page

home.php <a href='javascript:void(0);' onclick="pepz('<?php echo"ppc-mngt_transfer.php?id=$i->sm_id"; ?>', 'myPop1',400,400)"><?php print"$i->sm_jo<br>$i->sm_custo_name"; ?></a> ppc-mn ...

How to transfer data between components in Angular 6 using a service

I'm facing an issue with passing data between the course-detail component and the course-play component. I tried using a shared service and BehaviorSubject, but it didn't work as expected. Strangely, there are no errors thrown, and the data remai ...

Determination of the sum in an array

I'm currently facing an issue with calculating the total numbers in an array. Whenever a new value is added to the array, the total should be automatically updated on the view. However, I'm encountering an undefined error during this process. va ...

What could be causing the error when my file is running?

Whenever I attempt to run a file using the command node database.ts, an error pops up. Can someone help me identify what's wrong with my syntax? This is how the file appears: import { Sequelize } from 'sequelize-typescript'; export const ...

Creating a script for an automated clicker that targets a specific link on a webpage using Chrome

With my limited coding experience, I find myself at a loss on how to tackle this task. I am attempting to navigate Fortune Tech to catch up on news from the majority of 2016. However, I encountered a link that appears when you reach the end of the page, p ...

Revamping a user interface to handle clicks and display using Blazor instead of JavaScript

As a newcomer to Blazor, I find myself searching for the most efficient method to replicate a JavaScript-based click and display user interface that I had previously created. The code snippet for the page is as follows: <div class="container"> & ...

Obtaining the plugin directory URL through ajax in Wordpress

I have seen this question asked before, but the solution provided does not seem to work for me. I am currently using WordPress and I am attempting to specify the location of the URL "myfile.php" within my plugin directory. However, I am unsure if this is e ...

Improve the performance of your three.js project by utilizing JavaScript arrays instead of importing OBJ or GLTF files

I've noticed that loading OBJ/GLTF/GLB models into my three.js application can be time-consuming. On the other hand, declaring JavaScript arrays of vertices and indices with the same data and creating a new object is much quicker (and reduces file siz ...

Steps for dynamically generating rows in an HTML table using information extracted from a database

Hey everyone, I have a MySQL database table named "mezziDiTrasporto" with multiple rows of data. I am trying to create a new table that will automatically populate rows with data from the database table whenever a SELECT query is executed. Is this achievab ...

Ensuring that the image perfectly fills the entire screen on an iPhone X using React Native

Looking for a solution to make my image fit the entire screen on the iPhone X simulator. I've tried adjusting the imageContainer width to "100%" and the container that encompasses everything, but haven't had any luck. Would appreciate any suggest ...

Using Angular TypeScript to create dynamic CSS animations

Looking to incorporate CSS animations into a function. When the timer reaches its end, it triggers a snack bar message and I want the color of the timer text to shift from white to red as a warning signal. Currently, I have managed to turn it red at the ...

Switch up the header's date format on NGX-Print

Within my application, I am utilizing ngx-print with Angular 8. When I click the print button, a date in the format mm/dd/yy is added to the header. However, I would like to change it to dd/mm/yy. Despite searching, I have been unable to find any document ...

Show different components depending on immediate modifications and complete or incomplete data entry in Vue.js

I am trying to create a simple form with a search bar and two buttons. The goal is for the fill button to be visible only when there is text in the input, and for the empty button to show when the input is empty. I want the buttons to dynamically display o ...

Issue with fadeIn effect not functioning properly on the second child element

Requesting assistance with troubleshooting my code. The intended functionality is to animate the img tags using fadeIn and fadeOut, but currently only the first image fades out without the second image fading in. I suspect there may be an issue in my CSS t ...

Using PHP to output a string within a JavaScript onclick function parameter

I am attempting to display a string in PHP as a string variable within a JavaScript function, but it is not appearing correctly. How can I resolve this issue? <?php $example1 = "example 1"; $example2 = "example 2"; $examp ...

The disappearance of UI elements in Angular 13 and Bootstrap 5 when new routes are introduced

After spending a considerable amount of time on website development, I have hit a roadblock with the navigation. Whenever I set up a route, the entire user interface disappears and refuses to load. I have searched extensively but found no solution to this ...

Angular = encountering an incorrect array size limitation

I am encountering an issue with the function in my controller... $scope.pagerPages = function (n) { var i = Math.ceil(n); return new Array(i); } The n value is derived from an expression on the view and can sometimes be a fraction. This is why I ...

The extent of utilizing the click handler in ECMA script 6

In ES6 arrow functions, the scope of this becomes available. However, there are instances where I cannot access this in an arrow function while it works with a normal anonymous function. For example: Sample 1 $(document).ready(function() { $(' ...

Adding a MTL texture to an OBJ in your three.js project is a simple process that can enhance

Hello! I am currently working on adding an MTL file to my OBJ in three.js. I had successfully loaded my OBJ and went back to address this issue. However, after adding the code to load the MTL file using MTLLoader, the code seems to be getting stuck at mt ...

What is the most efficient method for iterating through an array stored in JSON format?

Looking for help with parsing a JSON file that contains a list of departures. I want to loop through each bus line and display them in separate div elements like this: <div class="bus"> <div class="line">departure.line</div> < ...