Issue encountered while creating a Telegram Bot: Firebase Cloud Functions throwing an error message of "TypeError: Cannot read property 'slice' of undefined"

Greetings! I am currently in the process of developing a Telegram bot using Firebase cloud functions with Typescript.

Despite my bot successfully executing its tasks, there seems to be an issue that is keeping my cloud functions persistently active, leading to an error as indicated in the title. It's puzzling how this error continues to occur even after the function has completed its task successfully.

This ongoing issue is not only filling up my cloud function logs but also increasing the count of function invocations.

https://i.sstatic.net/fg1SO.png

The problem persists, seemingly in a loop...

Here is the snippet of my code where the user command is received and split into two parts if it consists of two words:

app.post('/', async (req, res) => {

  const isTelegramMessage = req.body
                          && req.body.message
                          && req.body.message.chat
                          && req.body.message.chat.id
                          && req.body.message.from
                          && req.body.message.from.first_name

  if (isTelegramMessage) {
    // Command processing logic
  }

  return res.status(200).send({ status: 'not a telegram message' })
})

export const router = functions.https.onRequest(app)

EDIT: Despite the successful completion of my function, the error continues to occur. Strangely, these errors persist even when I haven't sent any Telegram messages, indicating that the function is being invoked unnecessarily.

https://i.sstatic.net/9DOZI.png

Although the bot functions as intended, the error in question is causing an increase in function invocations.

https://i.sstatic.net/rjehX.png

Number of function invocations since the onset of this issue.

https://i.sstatic.net/huZDx.png

Answer №1

One of the reasons for this distinction is that in JavaScript, null is not equal to 'undefined'.

So instead of

if (userInput !== null) 

it is recommended to use

if(typeof userInput !== 'undefined')

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

Uncovering TypeScript's Type Inference Power Through the keyof Keyword

Recently, I encountered a situation where I needed to utilize an abstract class. export abstract class ABaseModel { public static isKeyOf<T>(propName: (keyof T)): string { return propName; } } Following that, I also created another class wh ...

React - Component not updating after Axios call in separate file

Recently I decided to delve into React while working on some R&D projects. One of my goals was to build an application from scratch as a way to learn and practice with the framework. As I started working on my project, I encountered a rather perplexin ...

Sending an array of strings to the function is not allowed

I'm encountering an issue with the following function: function proc(unames: Array<string>){} When I attempt to pass the function the following input: import _ = require('lodash'); const usernames = _.flattenDeep([unames]).filt ...

Are there any other methods besides @ViewChild to access template elements by template ID in Angular v4.3.3?

In the past, using @ViewChild('#templateId') was an accepted method for obtaining an Element Reference. @ViewChild('#templateId') elementName: ElementRef; However, it appears that this is no longer a viable approach as @ViewChild now ...

Angular does not permit the use of the property proxyConfig

Click here to view the image I encountered an issue when attempting to include a proxy config file in angular.json, as it was stating that the property is not allowed. ...

Functional programming: Retrieve the initial truthy output from executing an array of diverse functions using a particular parameter

As I delve into the world of functional programming in TypeScript, I find myself contemplating the most idiomatic way to achieve a specific task using libraries like ramda, remeda, or lodash-fp. My goal is to apply a series of functions to a particular dat ...

Guide to implementing Apollo GraphQL subscriptions in NextJS on the client-side

As a newcomer to NextJS, I am facing the challenge of displaying real-time data fetched from a Hasura GraphQL backend on a page. In previous non-NextJS applications, I successfully utilized GraphQL subscriptions with the Apollo client library which levera ...

Upgrade from Next.js version 12

Greetings to all! I have recently been assigned the task of migrating a project from next.js version 12 to the latest version. The changes in page routing and app routing are posing some challenges for me as I attempt to migrate the entire website. Is ther ...

Is it possible to combine TypeScript modules into a single JavaScript file?

Hey there, I'm feeling completely lost with this. I've just started diving into Typescript with Grunt JS and I could really use some assistance. I already have a Grunt file set up that runs my TS files through an uglify process for preparing the ...

Is there a way to dynamically define the return type of a function in Typescript?

Can the variable baz be dynamically assigned the string type? type sampleType = () => ReturnType<sampleType>; // Want to return the type of any function I pass (Eg. ReturnType<typeof foo>) interface ISampleInterface { baz: sampleType; } ...

Google Cloud PubSub does not automatically resend unacknowledged messages

The answer chosen for this particular question contains some pertinent details I currently have a subscription set up with the following parameters: https://i.stack.imgur.com/Bn0d4.png along with the following code snippet: const subscription = this.pub ...

What is the best way to incorporate resources from a different location in an Angular project?

I am facing an issue with the deployment time of my server as I am using @angular/localize to support three languages in my application. Despite all locales sharing the same assets, they are being downloaded and deployed individually for each one. To addr ...

Having trouble changing the query string in the URL with Angular 4?

My search form includes various filters such as text inputs, checkboxes, and radio buttons. Whenever the user interacts with these filters, the query string in the URL needs to be updated. Consider the following scenario: http://example.com/search?myFilt ...

The problem with the Custom Select Component: Error Arises When Utilizing the Generic Type <T,> with a Comma as Opposed to <T> Without a Comma

I recently developed a unique custom select component that extends the standard HTML select element. During the process, I made use of a generic type to accommodate a wide range of data types, but encountered an unexpected error. The issue seems to persist ...

What is the correct way to interpret a JSON file using TypeScript?

Encountering Error Error TS2732: Cannot locate module '../service-account.json'. It is suggested to use the '--resolveJsonModule' flag when importing a module with a '.json' extension. import serviceAccountPlay from '../ ...

Tips for finalizing a subscriber after a for loop finishes?

When you send a GET request to , you will receive the repositories owned by the user benawad. However, GitHub limits the number of repositories returned to 30. The user benawad currently has 246 repositories as of today (14/08/2021). In order to workarou ...

How do I create a standalone .ts file with Angular 9?

Just diving into Angular development and eager to create a standalone .ts file without having to generate an entire component. Can anyone guide me on how to achieve this using ng generate? Scenario: During the application build process, I need to write th ...

Verify if a given string exists within a defined ENUM in typescript

I have an enum called "Languages" with different language codes such as nl, fr, en, and de. export enum Languages { nl = 1, fr = 2, en = 3, de = 4 } Additionally, I have a constant variable named "language" assigned the value 'de'. My g ...

Is it possible that updating AngularFire leads to scripts failing to function properly?

Having an issue with a script that reads data from Firebase using AngularFire, Firebase, and Angular. The problem arises when upgrading from version 0.7.1 to 0.8.0 of AngularFire, causing the script to stop working without clear indication of error. <b ...

Angular and Bootstrap work hand in hand to provide a seamless user experience, especially

I have been exploring ways to easily close the modal that appears after clicking on an image. My setup involves using bootstrap in conjunction with Angular. <img id="1" data-toggle="modal" data-target="#myModal" src='assets/barrel.jpg' alt=&a ...