在异步功能下无法访问此属性

时间:2019-11-26 03:47:04

标签: javascript

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:4953/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/Registrations/FindByCode?code=1234",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      }
    },
    "My.WebService20": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/Registrations/FindByCode?code=1234",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:4954/"
    }
  }
}

我只需要访问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); } } 中的名称-值。当我尝试记录名称值时,它无法打印。是否有其他用于访问名称值的过程,或者以上代码中是否存在缺陷?

2 个答案:

答案 0 :(得分:0)

如果您要访问的是分配给此行中定义的变量name的值:

var name = 'subash';

您只需要省略this的使用,因为内部函数getnearplaces可以访问包含在其上下文中的变量。

所以这个:

console.log('running', name)

应打印字符串:

running subash

为了更好地了解闭包在JavaScript中的工作方式,您可能需要阅读以下内容: MDN - Closures

希望这会有所帮助

答案 1 :(得分:0)

尝试将异步功能更改为箭头功能:

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);
  }
}

我希望这会有所帮助。