Call one class function from another function from the same class -

Getting Error " ERROR TypeError: Cannot read properties of undefined "   (i.e. 'this.' is not defined in class function)

App.component.ts 

class AppComponent {

   public setNodeTemplate(obj: NodeModel, diagram: Diagram): Container {

             let designator : string = (obj.data as EmployeeInfo).Designation;

             switch(designator) {

                   case 'desig1_working': {

                                          /**

                                            * if code to create content object is inline then code works 

                                           * but its getting rather large and want to pull it out to of teh switch statement and put 

                                           * in its own class function. 

                                           */

                                           //DO_LOTS_OF_STUFF   

                                          return content;

                                }

                   case 'desig2_not_working': {

                                            /**    * use wrapper function to keep case statement fn size small    

                                                        the problem is 'this' is undefined */

                                          let  content : StackPanel =this.setNodeTemplate_desig2(obj);

                                         return content;

                             }

// I have lots of case statements and the code is now getting unmanageable, so want to wrap up the code 

// in each case into its own class function.  


            }

    }

   //refactor code in case statement using VS CODE....

   private setNodeTemplate_desig2(obj: NodeModel)  {

        //ps: tried returning type Container

        return content ; 

  }

} //class


runtime error on inspect: 

ERROR TypeError: Cannot read properties of undefined (reading 'setNodeTemplate_desig2')


3 Replies 1 reply marked as answer

GD Gobinath Dhamotharan Syncfusion Team January 12, 2024 07:58 AM UTC

Hi,

In Angular, the issue of this being undefined arises when attempting to access the component before its creation. While the setNodeTemplate method is invoked before the component is created, one workaround is to access the same class method using a function declared outside the class. This external function can be utilized to achieve the desired behavior. Still, if you need to call the method with in the same class, could you please provide more details about your specific use case and what you are trying to achieve with the 'setNodeTemplate' method? This additional information will help in providing more tailored guidance. Refer the sample below for your reference.

Sample

https://stackblitz.com/edit/angular-7zxtvg-dfzpqq?file=app.component.ts


Regards,

Gobinath



IF Ian Finlay January 14, 2024 10:14 PM UTC

Thats great - Thanks Gobinath

for others looking at the example url: look at : function setNodeTemplate_desig2()


App.component.ts 

class AppComponent {

   public setNodeTemplate(obj: NodeModel, diagram: Diagram): Container {

             let designator : string = (obj.data as EmployeeInfo).Designation;

             switch(designator) {

                   case 'desig1_working': {

                                          /**

                                            * if code to create content object is inline then code works 

                                           * but its getting rather large and want to pull it out to of teh switch statement and put 

                                           * in its own class function. 

                                           */

                                           //DO_LOTS_OF_STUFF   

                                          return content;

                                }

                   case 'desig2_not_working': {

                                            /**    * use wrapper function to keep case statement fn size small    

                                                        the problem is 'this' is undefined */

                                          let  content : StackPanel =this.setNodeTemplate_desig2(obj);

                                         return content;

                             }

// I have lots of case statements and the code is now getting unmanageable, so want to wrap up the code 

// in each case into its own class function.  


            }

    }


   //refactor code in case statement using VS CODE....

function setNodeTemplate_desig2(objNodeModel): StackPanel {

        //ps: tried returning type Container

        return content ; 

  }


} ///class



SJ Sivaranjith Jeyabalan Syncfusion Team January 15, 2024 04:53 AM UTC

Hi Ian,

We hope the solution meets your requirements. As a result, we're marking it as resolved. If you have any additional queries or concerns, feel free to reach out to us.

Regards,

Sivaranjith


Marked as answer
Loader.
Up arrow icon