Uncaught TypeError: this.lable is not a function

i want to call my function inside another function
and i get this error Uncaught TypeError: this.lable is not a function



public lable()
{
  console.log('helllo')
}

 autoUpdate() {


    var geolocation = new  Geos({
          projection: map.getView().getProjection(),
          tracking: true,
          trackingOptions: {
            enableHighAccuracy: true,
            maximumAge: 2000  
          }
        });
geolocation.on('change', function() {
  
          var pos = geolocation.getPosition();
     
           this.position = geolocation.getPosition();
           this.accuracy = geolocation.getAccuracy();
           this.heading = geolocation.getHeading() || 0;
           this.speed = geolocation.getSpeed() || 0;

          this.lable();

          
      
   
          rome.setGeometry(new Point(pos));
          view.setCenter(pos);
         // view.setZoom(18); 
        })
}

Thanks & Regards

Hi @youssef_tdi!

Have you tried writing the lable method with the opening curly brace on the same line? Like this:

public lable() {
  console.log('helllo')
}

Semicolons are optional in Javascript and it may not be taking the code block below as you expect.

Best,
Rodrigo

Never type the word function inside the body of one. Read this for more information.

1 Like

@rapropos is right, I missed that line. By using a normal function instead of an arrow function, the inner function doesn’t have access to the enclosing scope, and therefore this.lable is undefined (a.k.a. not a function).

Change that line, and it should work:

geolocation.on('change', () => {

Also: Your code need some serious indentation, it’s hard to read :sweat_smile:

1 Like

its work Thanks @FdezRomero :+1:

i want to call my variable inside function @FdezRomero @rapropos

 geolocation.on('change', ()=> {
  
          var pos = geolocation.getPosition();
     
           this.position = geolocation.getPosition(); // this is my global variable was i declared in the top
           this.accuracy = geolocation.getAccuracy();
           this.heading = geolocation.getHeading() || 0;
           this.speed = geolocation.getSpeed() || 0;

           const m = Date.now();
             
           this.addPosition(this.position, this.heading, m);
         
           const coords = this.positions.getCoordinates();
           const len = coords.length;
           console.log("coords",coords)
           if (len >= 2) {
             this.deltaMean = (coords[len - 1][3] - coords[0][3]) / (len - 1);
           }

       
      
     
          rome.setGeometry(new Point(pos));
          view.setCenter(pos);
         
        

         
          
         // view.setZoom(18); 
        })


        console.log("positions",this.positions);// i get variable undefined

Look carefully at the scoping you have written. The console.log statement at the bottom of your post is outside the handler passed to geolocation.on, therefore it is completely unrelated. Move it inside the handler and/or accept that this.positions is never going to be what you want it to at that point in your program.

1 Like

thank for reply @rapropos
can you give an example about this problem ?