Home  >  Forum  >  Angular
Post New Query

How to convert array to tree in angular 4



Asked on April 20, 2018
Hello,

I am referring following document to convert to array to tree
https://npm.runkit.com/array2tree

In my component.ts I have written like below
getLocation() {
this.locationService.getAllLocations()
.subscribe(
data =>{
this.allLocations = data;
const tree=arrayToTree(this.allLocations,{ id: 'pk_location_id', parentId: 'parent_location_id' });
this.items =tree;
},
errorCode => this.statusCode = errorCode);
} 

In my Service file I have written like below
getAllLocations(): Observable<Location[]> {
return this.http.get(locationGet)
.map(this.extractData)
.catch(this.handleError);

}

I am getting following exception
Object(...) is not a function

please kindly any one help me out

Regards
Amruta Wandakar



Replied on April 20, 2018
You need to post complete error stacktrace.


Replied on April 21, 2018
@Jeet

From the service I get data in the following format
[{"pk_location_id":1,"locationname":"Oman","parent_location_id":0,"has_children":true},
{"pk_location_id":2,"locationname":"Muscat","parent_location_id":1,"has_children":true},
{"pk_location_id":3,"locationname":"Seeb","parent_location_id":1,"has_children":false},
{"pk_location_id":4,"locationname":"Ruwi","parent_location_id":2,"has_children":false}]

The following is the stack trace

ERROR TypeError: Object(...) is not a function
    at SafeSubscriber._next (dropdown-treeview-select-demo.component.ts:91)
    at SafeSubscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:239)
    at SafeSubscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.SafeSubscriber.next (Subscriber.js:186)
    at Subscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.Subscriber._next (Subscriber.js:127)
    at Subscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.Subscriber.next (Subscriber.js:91)
    at CatchSubscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.Subscriber._next (Subscriber.js:127)
    at CatchSubscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.Subscriber.next (Subscriber.js:91)
    at MapSubscriber.webpackJsonp.../../../../rxjs/_esm5/operators/map.js.MapSubscriber._next (map.js:85)
    at MapSubscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.Subscriber.next (Subscriber.js:91)

Regards
Amruta




Write Answer




©2019 concretepage.com | Privacy Policy | Contact Us