简体   繁体   English

未定义 object 属性,但有时在我刷新页面或稍后在代码中定义

[英]An object property is not being defined, but it sometimes is defined when I refresh the page or later in the code

Hey there I have this piece of code that gets the users geolocation and stores that in a location property of a Data class, simple enough.嘿,我有这段代码可以获取用户的地理位置并将其存储在数据 class 的位置属性中,非常简单。

When I use the default constructor for this function I want it to call the function当我为此 function 使用默认构造函数时,我希望它调用 function

_initLocation()

To define the _location property.定义 _location 属性。

This is the code for the object:这是 object 的代码:

  constructor(){
    this._initLocation();
    this._dataStore = new Map();
  }
  
  _initLocation(){
    if(navigator.geolocation){
      navigator.geolocation.getCurrentPosition((position) => {
      const { latitude, longitude } = position.coords;
      this._location = new Array(latitude, longitude);

      }, () => alert('location not found'))
    }
  }

  get location(){
    return this._location;
  }

  get dataStore(){
    return this._dataStore;
  }

  icon = L.icon({
    iconUrl: 'icon.png',
    
    iconSize: [100, 100],
    iconAnchor: [50, 100],
    popupAnchor: [0, -100],
  });

}

When I use this same code outside of the Object, it works perfectly fine, but whenever I try to use console.log on the newly created object I get:当我在 Object 之外使用相同的代码时,它工作得非常好,但是每当我尝试在新创建的 object 上使用 console.log 时,我得到:

Object { icon: {…}, _dataStore: Map(0) }
​
_dataStore: Map(0)
​
icon: Object { options: {…}, _initHooksCalled: true }
​
<prototype>: Object { … }

So there is no location property on this object, but later on if I console.log the object that property will later exist.因此,此 object 上没有位置属性,但稍后如果我 console.log 记录 object 该属性稍后将存在。 It also sometimes appears on refresh.它有时也会在刷新时出现。 It's ruining code later in the project because that location data is required but undefined.它在项目后期破坏了代码,因为该位置数据是必需的但未定义。 It's clear I need some delay or lag?很明显我需要一些延迟或延迟? I've tried DOMContentLoaded.我试过 DOMContentLoaded。 But I'm also uncertain as to why the alert popup isn't showing if it can't get the current location?但我也不确定如果无法获取当前位置,为什么警报弹出窗口没有显示?

Also frustratingly when I console.log the location value in the _initLocation() function I get the correct data, but if I console.log the _location property I get 'undefined'??同样令人沮丧的是,当我 console.log _initLocation() function 中的位置值时,我得到了正确的数据,但是如果我 console.log _location属性,我得到'未定义'?

Any help would be greatly appreciated.任何帮助将不胜感激。

Edit, here is how I'm using that object:编辑,这是我使用 object 的方式:

I instantiate the object then I used it in an object named Renderer, by assigning it to _maptyData property.我实例化了 object,然后通过将其分配给 _maptyData 属性,在名为 Renderer 的 object 中使用它。 I then use that property in these functions:然后我在这些函数中使用该属性:

  createMap(){
    this.map = L.map('map')
    .setView(this.maptyData.location, 13);

    L.tileLayer('https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', {
      attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'})
      .addTo(this.map);
  }
  
  createMarker(atLocation = maptyData.location){
    if(atLocation === maptyData.location) {
      console.log(maptyData._location);
      this._locMarker = L.marker(atLocation, {icon: maptyData.icon})
      .addTo(this.map)
      .bindPopup('Your current location')
      .openPopup();
      console.log(this._locMarker);
    } else {
      this.currentMarker = L.marker(atLocation, {icon: maptyData.icon})
      .addTo(this.map)
      .bindPopup('Please enter event information!')
      .openPopup();
      return this.currentMarker;
    }

I resolved this problem, I'm new to javascript and later realised the problem is because the code needs to be async, I learnt about promises and implemented them to fix this.我解决了这个问题,我是 javascript 的新手,后来意识到问题是因为代码需要异步,我了解了 Promise 并实现了它们来解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM