Cannot read properties of undefined (reading 'isDestroyed')

Tags: #<Tag:0x00007f8b19edcf78> #<Tag:0x00007f8b19edce38>

Hello,

We are upgrading Handsontable from 8.4.0 to 13.1.0.
Our app is using Angular framework (version 16).
With the new package the exception “TypeError: Cannot read properties of undefined (reading ‘isDestroyed’)” is being thrown in various places.

I have tracked down issue to the HotTableRegisterer().getInstance(id) method – it does throw that exception.

Note: there is no such issue when using version 8.4 of Handsontable.

Here’s the code which demonstrates an issue.

import { Component } from ‘@angular/core’;

import { OnInit } from ‘@angular/core’;

import { HotTableRegisterer } from ‘@handsontable/angular’;

import Handsontable from ‘handsontable/base’;

class TableItem {

constructor(public uid: number, public name: string, public alias: string) {

}

}

@Component({

selector: 'app-root',

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent implements OnInit {

hotID: string = 'hot-317';

tableData: TableItem[] | undefined = [];

constructor() {

    // this fails

    try { this.getHotInstance(); console.log('getHotInstance(1): OK'); } catch(x) { console.error('Error(1):', x); }

}

ngOnInit(): void {

    // this fails

    try { this.getHotInstance(); console.log('getHotInstance(2): OK'); } catch(x) { console.error('Error(2):', x); }

    // only this succeeds

    setTimeout(() => {

        try { this.getHotInstance(); console.log('getHotInstance(3): OK'); } catch(x) { console.error('Error(3):', x); }

    });

}

public getHotInstance(): Handsontable {

    return new HotTableRegisterer().getInstance(this.hotID);

}

}

Hi @igort

It would hard to narrow down the change that is causing this, but the main issue here is that ngOnInit() hook runs before Handsontable instance is initialized, and that’s the reason why it throws an error. To solve this I would recommend to use ngAfterViewInit() to avoid this problem.

Here’s an example: https://jsfiddle.net/handsoncode/yx0bquv7/

Thank you Adrian for reply and suggestion.
I have deployed a different solution already.
I just wanted to point out on that breaking change since HOT 8.4.

Of course, thank you for this. Can you also share your solution? I’m sure it will be helpful for other users that might encounter the same issue.

I just wrapped method which was using getInstance() into try/catch block.
That worked for me because it was some periodic action so initial call silently failed but after that everything was working as expected.
Obviously, that won’t work for initialization type of work.

@igort

Ok, I see, thank you :slight_smile: