angular-tree-component
angular-tree-component copied to clipboard
Changing the reference to nodes will report "ERROR TypeError: Cannot read property 'data' of null"
Minimal reproduction of the bug/regression with instructions:
https://stackblitz.com/edit/angular-tree-component-demo?file=src%2Fapp%2Fapp.component.html
Click the "update tree nodes" button will see the error, But the tree nodes are updated success.
Expected behavior:
No Error report.
Versions of Angular Tree Component, Angular, Node, affected browser(s) and operating system(s):
@circlon/angular-tree-component ^10.0.0
@angular-devkit/architect 0.1100.2
@angular-devkit/build-angular 0.1100.2
@angular-devkit/core 11.0.2
@angular-devkit/schematics 11.0.2
@angular/cdk 11.0.1
@angular/flex-layout 10.0.0-beta.32
@angular/material 11.0.1
@angular/material-moment-adapter 11.0.1
@schematics/angular 11.0.2
@schematics/update 0.1100.2
ng-packagr 11.0.3
rxjs 6.5.5
typescript 4.0.5
system:
MacOS v10.14.6
Other information:
The error threw in node.isRoot
method;
I would be willing to submit a PR to fix this issue
I'm not sure. [ ] Yes (Assistance will be provided if you need help to submit a pull request) [ ] No
The error is reported in:
// projects/angular-tree-component/src/lib/models/tree-node.model.ts 70:45
get isRoot(): boolean { return this.parent.data.virtual; }
Modify to:
get isRoot(): boolean { return this.parent?.data.virtual; }
Still not sure that it's OK, but instead of node.IsRoot you can write node.parent == null || node.parent.data.virtual Here is my workaround:
<div *ngIf="(node.parent == null || node.parent.data.virtual)" class="inline p-w-xxs">...</div>
The error is reported in:
// projects/angular-tree-component/src/lib/models/tree-node.model.ts 70:45 get isRoot(): boolean { return this.parent.data.virtual; }
Modify to:
get isRoot(): boolean { return this.parent?.data.virtual; }
This is required since node.setActiveAndVisible() method internally calls node.isRoot which gives error.
Facing same issue.. Any fix or workaround to this?
One thing can be done is to call
node = this.tree.treeModel.getNodeById(node.id)
and then go on with your operations