0
lookforcustomers(value){
  if (value == 'on') {
    var name = 'subash';

    this.intervalid = setInterval(async function getnearplaces(nameref) {
      console.log('running', this.name)

      //  console.log('loc',outerthis.state.lastknownlocation.latitude)

      // var driverpoint = new Parse.GeoPoint({latitude:this.ref.state.lastknownlocation.latitude, longitude:this.ref.state.lastknownlocation.longitude});
      //  var GameScore = Parse.Object.extend("Booking");
      // const query = new Parse.Query(PlaceObject);
      // query.equalTo("hired",false);
      // query.near("pick", driverpoint);
      // query.limit(1);
      //  const placesObjects = await query.find().then((object) => {
      //    console.log(object[0])
      //  }, (error) => {
      //
      //  console.log('error while looking for the customers')
      //  });

    }, 10000);
  }
}

I just need to access name-value from getnearplacesfuntion(). When I try to log the name value it is not printing. Is there any other procedure to access name value or is there any flaw in code above?

Tal Z
  • 3,170
  • 17
  • 30
subhashchandru
  • 97
  • 1
  • 10
  • Did you try to call `this.intervalid()`? – Thanh Dao Nov 26 '19 at 04:12
  • One more problem, you are calling this into `getnearplaces()` function, so `this` keyword represents for that. If you want to call like this, you should bind the keyword to the function `getnearplaces.bind(this)`. Sorry for my bad English – Thanh Dao Nov 26 '19 at 04:14

2 Answers2

0

If you mean you want to access the value assigned to the variable name defined in this line:

var name = 'subash';

You simply need to omit the use of this as the inner function getnearplaces has access to variables defined in the context it is enclosed in.

So this:

console.log('running', name)

should print the string:

running subash

For better understanding of how closures work in JavaScript, you might want to read this: MDN - Closures

Hope this helps

Tal Z
  • 3,170
  • 17
  • 30
0

Try changing the async function to a arrow function:

lookforcustomers(value){
  if (value == 'on') {
    var name = 'subash';

    this.intervalid = setInterval(async (nameref) => {
      console.log('running', this.name)

      //  console.log('loc',outerthis.state.lastknownlocation.latitude)

      // var driverpoint = new Parse.GeoPoint({latitude:this.ref.state.lastknownlocation.latitude, longitude:this.ref.state.lastknownlocation.longitude});
      //  var GameScore = Parse.Object.extend("Booking");
      // const query = new Parse.Query(PlaceObject);
      // query.equalTo("hired",false);
      // query.near("pick", driverpoint);
      // query.limit(1);
      //  const placesObjects = await query.find().then((object) => {
      //    console.log(object[0])
      //  }, (error) => {
      //
      //  console.log('error while looking for the customers')
      //  });

    }, 10000);
  }
}

I hope this helped.

Laurent Dhont
  • 1,012
  • 1
  • 9
  • 22