"An issue arises as the variable this.results.rulesFired is not properly

I am faced with a set of table rows

<tr *ngFor="let firedRule of splitRules(results.rulesFired); let rowNumber = index"
            [class.added]="isAdd(firedRule)"
            [class.removed]="isRemove(firedRule)"
            [class.modified]="isModify(firedRule)">

The splitRules function is responsible for breaking down rules into new properties:

splitRules(firedRules: FiredRule[]) {
const regExRowNumber = /row\s*([0-9]+)/;
let rowMatch = [];
firedRules.forEach(rule => {
  let name = '';
  name = rule.ruleName.toLowerCase();
  rowMatch = regExRowNumber.exec(name);
  const rowNumber = rowMatch[1];
  const ruleName = name.substr(rowMatch[0].length).split('-')[0];
  const conditionsMatched = name.substr(rowMatch[0].length).split('-')[1];
  rule.groupName = ruleName.trim().replace('!', ': ').split(':')[0];
  rule.definitionName = ruleName.trim().replace('!', ': ').split(':')[1];
  rule.rowNumber = rowNumber;
  rule.conditionsMatched = conditionsMatched.trim();
  rowMatch = [];
});
return firedRules;

}

It's advised not to call the splitRules method within html, so I attempted calling it on ngOnInit:

@Input() results: RulesEngineResponse;
ngOnInit(): void {
   this.results.rulesFired = this.splitRules(this.results.rulesFired);
}

However, upon removing the call from html, an error

ERROR TypeError: Cannot read property 'rulesFired' of null
occurred. Is there a way to resolve this issue?

Your assistance would be greatly appreciated.

Answer №1

One possible reason for this issue could be that the Input() results value is being passed as null from its parent component. Ensure that you are passing a valid value to the Input() results in that component. If there is a situation where you may pass the Input() results value at a later time, remember to perform a null check before utilizing this.results.

Answer №2

In order to properly define the results variable as an object and initialize it in either the constructor or ngOnInit based on your specific use cases, you can do so by following this example:

class myComponent implements OnInit {
  public results: object<{rulesFired: Array<any>}>;

  constructor() {
   this.results = {rulesFired: []}
  }
   
}

Answer №3

It's pretty straightforward. The variable this.results is currently not defined.

Update: Considering that it's an input, it appears that the main component is passing a null value to result. You may want to set up its initialization in the main component.

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 calculating the total sum of inner object property values using JavaScript, TypeScript, or Angular 5

What is the total sum of successCount values in the given array object? var successCount;//I want count of all successCount attributes from the below object var accordianData = [ { name: "Start of Day", subItemsData: [ { title: " ...

Verify if the specified key is present in the type parameter and if it corresponds to the expected data type in the value parameter

I have a specific goal in mind: export interface GCLPluginConfig { [key: string]: string | number | boolean | Date | string[]; } export interface CorePluginConfig extends GCLPluginConfig { "core.lastUpdateCheck": Date; } export interface An ...

No information is being emitted by the subject

In my application, I have a feature where users input data that needs to be validated in a form. Once the validation is successful, a button is enabled allowing the user to submit their order. However, I'm facing an issue with this specific component ...

Keep an ear out for updates on object changes in Angular

One of my challenges involves a form that directly updates an object in the following manner: component.html <input type="text" [(ngModel)]="user.name" /> <input type="text" [(ngModel)]="user.surname" /> <input type="text" [(ngModel)]="use ...

Looking for guidance on where to find a functional code sample or comprehensive tutorial on working with ViewMetadata in Angular2

I am currently trying to understand the relationship between viewmetadata and the fundamental use of encapsulation: ViewEncapsulation, including ViewEncapsulation.Emulated and ViewEncapsulation.None. Here is a link for further information: https://angula ...

Exciting ngClass variation

I am facing a challenge of adding multiple classes to an element, with one of them being conditional. After referencing the documentation, I came across this method: <some-element [ngClass]="{'first': true, 'second': true, 'thi ...

Issue with bundling project arises post upgrading node version from v6.10 to v10.x

My project uses webpack 2 and awesome-typescript-loader for bundling in nodejs. Recently, I upgraded my node version from 6.10 to 10.16. However, after bundling the project, I encountered a Runtime.ImportModuleError: Error: Cannot find module 'config ...

Using Angular and RxJS5 to refresh data from both HTTP requests and sockets

Here's a specific scenario I need to address: Retrieve route parameters Utilize route parameters to make an HTTP service call Upon receiving a response from the HTTP service, use the same route parameters to invoke a socket service Whenever the sock ...

Having trouble getting combineLatest to properly normalize data in Angular using RxJS

Difficulty arose when attempting to normalize data prior to merging it into a new stream, resulting in no changes. I need to verify whether the user has liked the post using a Promise function before including it in the Posts Observable. this.data$ = comb ...

Exploring the world of mocking tests using Jest and inputs

Is there a way to create a jest test specifically for this function? const input = require('prompt-sync')(); export function choices(): void { const choice = input("Choose a letter"); if (choice === "a") { con ...

Enhance the Error class in Typescript

I have been attempting to create a custom error using my "CustomError" class to be displayed in the console instead of the generic "Error", without any success: class CustomError extends Error { constructor(message: string) { super(`Lorem "${me ...

Having trouble integrating Azure with my Ionic2/Angular2/Cordova app

I am currently developing with the Ionic2 Blank app template. I encountered an issue when trying to set up Azure using the following line of code in home.ts: import azureMobileClient from 'azure-mobile-apps-client'; . The deployment process wen ...

Exploring the depths of nested objects within HTML for Angular programming

I am currently working with a data structure that is defined as follows: app_list = ["app_1", "app_2"] data["results"] = { "app_1": [ {"key":1}, {"key":2} ], "app_ ...

How can I implement email authentication in Angular using Firebase?

After only a month of playing around with Angular, I have managed to develop a web app with login and signup forms using Firebase for data storage and authentication. Typically, new users are required to sign up before logging in. However, I now face a di ...

Is it possible that React.createElement does not accept objects as valid react children?

I am working on a simple text component: import * as React from 'react' interface IProps { level: 't1' | 't2' | 't3', size: 's' | 'm' | 'l' | 'xl' | 'xxl', sub ...

What is the prescribed interface or datatype for symbol type in TypeScript with JavaScript?

I have a set of symbol values in JavaScript that I want to convert to TypeScript. // Defining object values in JavaScript const size = { Large: Symbol('large'), Medium: Symbol('medium') } What is the most efficient method to conv ...

Retrieve category descriptions in Angular using their corresponding IDs

During the edit form process, I am sending categories by ID but displaying them as descriptions in options. Here is an example: new-campaign.html <mat-label>categories</mat-label> <mat-select multiple formControlName="VehicleCa ...

Restricting the number of lines within a paragraph in Angular 2

Is there a method to limit the number of lines in a <p> tag and add an ellipsis (...) at the end? Using character count for truncation doesn't work well as the width of the element varies according to device screen size. ...

How can you type a collection of initialized class instances in Typescript when given an object containing classes?

Imagine having an object that resembles the following: const typeMap = {category1: Category1, category2: Category2} In this case, Category1 and Category2 refer to classes, and there could potentially be hundreds of different categories. Now I also have a ...

Accessing properties using bracket notation in Typescript is a powerful feature that

I wish to retrieve a typed object using bracket notation like this: interface IFoo { bar: string[]; } var obj: IFoo = { bar: ["a", "b"] } var name = "bar"; obj[name]. // type information lost after dot As per the specification 4.10, it seems to be t ...