kianoush dortaj
kianoush dortaj

Reputation: 499

Cannot read property 'push' of undefined in angular 8

I need to push item in my list .

This is model to need add in list .

export interface OfflineLessonList {

    id: number;
    title: string;
    courseId: number;
    coursetitle: string;
    published: boolean;
    payType: PayTypes;
    parentId: number;
    displayOrder: number;
    childes: OfflineChildLessonList[];

}

export interface OfflineChildLessonList {

    id: number;
    title: string;
    courseId: number;
    coursetitle: string;
    published: boolean;
    payType: PayTypes;
    parentId: number;
    displayOrder: number;

}

i try to use this code :

treeToFlat(lessons: OfflineLessonList[]): OfflineLessonList[] {

    let data = new Array<OfflineLessonList>();

    lessons.forEach(parent => {

        let model = {} as OfflineLessonList;
        model.parentId = parent.parentId;
        model.payType = parent.payType;
        model.published = parent.published;
        model.title = parent.title;
        model.courseId = parent.courseId;
        model.coursetitle = parent.coursetitle;
        model.displayOrder = parent.displayOrder;

        if (parent.parentId === null) {

            data.push(model);

        } else {

            lessons.forEach(element => {
                let i = 0;
                if (parent.parentId === element.id) {
                    let find = data.find(x => x.id === parent.parentId);
                    find.childes = [];
                    find.childes.push(model);
                }
                i++;
            });

        }
    });

    console.log('in tree', data);

    // lessons.forEach(parent => {
    //  data.push(parent);
    //  parent['children'].forEach(lesson => {
    //      data.push(lesson);
    //      console.log(lesson);

    //  });
    // });
    return data;
}

but it show me this Error :

Cannot set property 'childes' of undefined

Whats the problem ? how can i solve this ?? ?

Upvotes: 0

Views: 136

Answers (4)

Adrita Sharma
Adrita Sharma

Reputation: 22213

Try like this:

let find = data.find(x => x.id === parent.parentId);

if(find){
    find.childes = [];
    find.childes.push(model);
}

Upvotes: 1

Shashank
Shashank

Reputation: 829

Add additional check

let find = data.find(x => x.id === parent.parentId);
if (find) {
  find.childes = [];
  find.childes.push(model);
}

Upvotes: 1

Ininiv
Ininiv

Reputation: 1325

let find = data.find(x => x.id === parent.parentId);

find value is undefined here

Return value of find method

The value of the first element in the array that satisfies the provided testing function. Otherwise, undefined is returned.

you are trying to set property for undefined value, hence it gives error

Upvotes: 1

C.OG
C.OG

Reputation: 6529

Your problem is with this code:

lessons.forEach(element => {
  let i = 0;
    if (parent.parentId === element.id) {
      let find = data.find(x => x.id === parent.parentId);
      find.childes = [];
      find.childes.push(model);
    }
    i++;
});

There are cases where let find = data.find(x => x.id === parent.parentId); will return undefined if it is unable to find a record. So you need to handle that case.

Consider checking the value exists before attempting to push:

lessons.forEach(element => {
  let i = 0;
    if (parent.parentId === element.id) {
      let find = data.find(x => x.id === parent.parentId);
      if (find) {
        find.childes = [];
        find.childes.push(model);
      }

    }
    i++;
});

Upvotes: 1

Related Questions