简体   繁体   中英

Javascript / API Blank Resolved Promise and Non Global Data from Request

I'm trying to set then grab a session storage variable that is returned from an API I'm using. The API is returning the proper values, and these values are accessible within the function which returns the result and its status, but not outside of the request. The request:

var geom;

var latArray = new Array();

var longArray = new Array();

function getLatLong(placeIdent) {

service = new google.maps.places.PlacesService(document.createElement('div'));

request = {placeId: ""+placeIdent+""};

service.getDetails(request, function(result, status) {

if (status !== google.maps.places.PlacesServiceStatus.OK) {

console.log(status);

} else {

  writeLatLongData(result);

  while(geom != "") {

    lat = geom.lat();
    latArray.push(lat);

    long = geom.lng();
    latArray.push(long);

    console.log(geom);
    console.log(lat);
    console.log(long);

    timeout();

  }

}

}

)

}

function writeLatLongData(result) {

  geom = result.geometry.location;

  return geom;
}

I know this function is the source of the problem, but if it's any help I'm attempting to access the stored values like so:

   function timeoutOne() {

    setTimeout(function(){ 

    getGeometry(placeArray);

    }, 1000); 

}

timeoutOne();

function getGeometry(placeArray) {

for (var j=0; j < nightsArray.length; j++) {

    getLatLong(placeArray[j]);
}

}

I'm pretty sure it's an async issue, but I'm not that familiar with Async/await and everything I've tried just returns an empty value when the promise is resolved (perhaps because the data are not accessible outside of the request function, still).

Does anyone have any suggestions?

What if below condition gets true,

status !== google.maps.places.PlacesServiceStatus.OK

your google places api call is getting failed (may be because you are making too much calls per 10 seconds), if this happens nothing will be returned which in turn making function getLatLong return undefined.

Check if at least once this function returning something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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