Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
Cannot read properties of undefined when chaining methods

I'm trying to compare lists using an extension method that calls a comparer like so:

type HasDiff<T> = (object: T, row: any) => boolean;

 export const isListEqualToRows = <T>(objects: T[], rows: any[], hasDiffFunction: HasDiff<T>): boolean => {
  if (objects.length !== rows.length)
    return true;

  return rows.every((row, index) => {
    const object = objects[index];

    if (row.isNew) {
      return false;
    }

    return !hasDiffFunction(object, row);
  });
};

The comparison is being triggered by Angular's value changed in a ReactiveForm, not sure if relevant, but here's it:

import { isListEqualToRows } from '@core/helpers';

formGroup.valueChanges.pipe(debounceTime(debounceValue)).subscribe((changes) => {
  this.areChangesDetected = !isListEqualToRows<Person>(this.items, changes, this.hasDiff);
});

My issue is that for one object, I have a list within a list. So I'm trying to call the isListEqualToRows() method again inside:

hasDiff(item: Person, row: any): boolean {
  return item.description !== row.description
    || !isListEqualToRows<Address>(item.addresses, row.addresses, this.hasDiffAddress);
}

hasDiffAddress(item: Address, row: any): boolean {
  return item.AddressId !== row.id
    || item.AddressStreet !== row.street;
}

But I end up getting this error while running the second line of the hasDiff() method:

Cannot read properties of undefined (reading 'hasDiffAddress')

How to solve this issue?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are calling hasDiff without binding it to the correct this object. Since you have strict mode enabled, the this that hasDiff is referring to when invoked outside of a someObject.hasDiff(...) syntax is then undefined (without strict mode it would be the global object).

Instead of passing this.hasDiff to isListEqualToRows, you need to pass either this.hasDiff.bind(this) or (item, row) => this.hasDiff(item, row).

Alternatively, add a thisArg argument to isListEqualToRows where you pass this from the caller's scope and then invoke hasDiffFunction as hasDiffFunction.call(thisArg, object, row) instead of hasDiffFunction(object, row).

See also: How does the "this" keyword work?

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!