Error: The property you are trying to set is undefined and cannot

When I attempt to set a property 'error' that is undefined, I receive a TypeError. The problematic line of code looks like this: this.error = error.code;

This issue arises in an Angular Reactive Form while making a call to a web service. Below is the relevant snippet:

@Component({
  // ...
})
export class SignUpComponent implements OnInit {
  // More declarations...
  error: string | null; // I also tried error = '' and error = null
  // Constructor, etc.

  public submit() {

    this.error = null;
    if (this.signUpForm.valid) {
      // Create an Auth0 user
      this.authService.signUp(this.signUp.value.email, this.signUp.value.password, this.authServiceCallback);
    }
  }

  public authServiceCallback(error) {
    if (error) {
      // This will cause an error child component to appear
      this.error = error.code;
    } else {
      // Do something else
    }
  }
}

I'm puzzled by what mistake I might be making. Any insights?

Answer №2

Another approach is to use arrow functions:

this.authService.signUp(this.signUp.value.email, this.signUp.value.password, x => this.authServiceCallback ( x (or any other parameter) ) );

Using arrow functions can make the reference to "this" more consistent.

If no argument is needed:

() => this.authServiceCallback ( )

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

Tips for incorporating a Forgot/Reset password option into your #Firebase platform

In the project I'm working on, I am utilizing #AngularFire2. My goal is to incorporate a Reset / Forgot password link into the login page. Does anyone have suggestions on how to accomplish this task? I'm looking to get some insights from #AskFi ...

"Error 404: The file you are looking for cannot be found on [custom company domain]. Please check

My attempts to retrieve a Google Drive file using its file ID with a service account in NodeJS have been unsuccessful. The requests are failing with an error indicating a lack of access: code: 404, errors: [ { message: 'File not found: X ...

Using ngrx store select subscribe exclusively for designated actions

Is it possible to filter a store.select subscription by actions, similar to how we do with Effects? Here is an example of the code in question: this.store .select(mySelector) .subscribe(obj => { //FILTER SUBSCRIPTION BY ACTION this.object = ob ...

Angular 6 - Resolving the Issue of 'Function calls are not supported in decorators' in Production Builds

Currently, I'm in the process of developing a cutting-edge Angular 6 application. However, as I was progressing with the development phase and tried to create a prod build using the following command: ng build --prod To my dismay, I encountered a pe ...

Validate the presence of a value in AngularFire and if not found, proceed to create it

I recently started using AngularFire, and since the update to AngularFire5, I've been encountering some issues. My goal is to create a 'room' with a unique id: name pair. When a user inputs a name, I need to check if that name already exists ...

When an attribute is used as a selector for a component in Angular, it can often trigger errors in

Looking to develop a component with an attribute as a selector, here's what I have: @Component({ selector: '[my-attribute-selector]', template: `` }) export class MyComponent { // Some cool stuff } Running into a tslint error t ...

I am struggling to extract data from the spawned Node.js child process. What am I overlooking?

Trying to utilize a spawned command-line process for lzip in order to expand an lzipped data stream due to the lack of suitable native JavaScript tools. Succeeded in achieving this by working with files and file descriptors, although cumbersome to handle ...

Angular application seamlessly connects with Nodejs application

In my Node.js application, I utilized Express generated by express generator and implemented handlebars as the view engine. The application operates smoothly on port 3000. The defined routes in Express include: ... app.use('/', index); app.use( ...

How to retrieve a parameter value within the app component in Angular 2

Within my appcomponent, I have incorporated a dropdown functionality. Whenever the user selects an option from the dropdown, it loads a new page in the router outlet. However, if I refresh the page, the router loads correctly but the dropdown selection i ...

The use of window.Image() is ineffective when working with TypeScript and React

In my React project, I am utilizing the KonvaJS library. You can find more information about it here. To display an image using JavaScript/React, I have implemented the following code: componentDidMount() { const image = new window.Image(); ima ...

Guide on how to address the problem of the @tawk.to/tawk-messenger-react module's absence of TypeScript definitions

Is there a way to fix the issue of missing TypeScript definitions for the @tawk.to/tawk-messenger-react module? The module '@tawk.to/tawk-messenger-react' does not have a declaration file. 'c:/develop/eachblock/aquatrack/management-tool-app ...

failure to render updated content after modification of variable

I am facing an issue with triggering a function in the component: componentA.ts html = 'hey'; this.onElementSelected(r => this.change()); public change() { console.log(this.html); if (this.html === 'hey&ap ...

Is it possible to use Typescript to store and access static global variables based on a unique key

I want to store variables in a static global file, like this: declare const MYVAR = 'Some unchanging data'; Later on, I would like to be able to retrieve the information using just the key 'MYVAR', for example: globalFile.findValue ...

Function overloading proving to be ineffective in dealing with this issue

Consider the code snippet below: interface ToArraySignature { (nodeList: NodeList): Array<Node> (collection: HTMLCollection): Array<Element> } const toArray: ToArraySignature = <ToArraySignature>(arrayLike: any) => { return []. ...

Assign the primeng dropdown's value to the model in a reactive form

I am currently encountering an issue while populating a form that contains several PrimeNg dropdowns. To simplify, let's consider an example similar to the ones provided on their website. <form [formGroup]="myFormGroup"> <p-dropdown [optio ...

Issue with sorting functionality in swimlane/ngx-datatable not functioning properly

Currently working with ngx-datatable version 11.2.0 and facing an issue related to client-side sorting on a column where the value is determined by an evaluated expression. The column contains numerical values representing latitude and longitude, but we ...

Pagination in Laravel using Angular 5

Currently experiencing pagination issues in an Angular5 and Laravel5 project. The JSON values are as follows: { "products": { "current_page": 1, "data": [ ... ], "first_page_url": "http://localhost:8000/api/ ...

Error encountered while utilizing Array.find with a specific data type

I am trying to locate the user's browser language from a list of supported languages. Here is the code snippet I am using: const userLanguage = browserLanguages.find(language => !!supported[language]); But unfortunately, I'm encountering thi ...

Modify information in formArray

Let's take a look at this setup I have: Model: export class MapDetailModel{ id: number; lat: number; lon: number; alt: number; long: number; angle: number; distance?: number; pendenza?: number; } Html: <div clas ...

What is the best way to filter out specific data fields from console.log in JavaScript?

When working with Java, I often use lombok to exclude certain fields from being printed. For instance, the @ToString.Exclude annotation can be used to prevent printing the user token. import lombok.ToString; public class TokenResponse { @ToString.Excl ...