Rawle Juglal February 2016

String value becomes undefined when trying to concat another string

I'm working on practicing with the openweathermap api. I have a coordinate object with keys lat & lon which are equal to a string. When I pass that coord obj into another function and try to concat those strings with the api call string they become undefined. I thought I made the scope of these variables global but it doesn't seem to be the case. Can someone tell me what is incorrect about this code

var apikey = '9575f3355ae129dc91424b5712a7695e';
var coords = {};
var accessOWM='';

function myLocation(){        navigator.geolocation.getCurrentPosition(function(position)       {
    coords.lat = (Math.round(position.coords.latitude*100)/100).toString();
    coords.lon = (Math.round(position.coords.longitude*100)/100).toString();
  });
}

function changeAccess(coordObj, key){
console.log(coordObj);
accessOWM ='http://api.openweathermap.org/data/2.5/forecast?lat='+coordObj['lat']+'&lon='+coordObj['lon']+'&APPID='+key;
}


myLocation();
console.log(coords);
changeAccess(coords, apikey);
console.log(accessOWM);

Answers


Maxim Gritsenko February 2016

You have an issue with async code. navigator.geolocation.getCurrentPosition(successCallback) function is an asyncronious function, the successCallback will not be executed immedeately, but with some delay. That is why when you call console.log(coords) and changeAccess(coords, apiKey), the coords are not defined yet. You need to call these functions (and the last one) from inside the .getCurrentPosition() callback.


Scott Marcus February 2016

Since coords is declared in the parent scope of changeAccess, you don't need to pass coordObj into changeAccess. Have you tried:

 accessOWM ='http://api.openweathermap.org/data/2.5/forecast?lat='+ coords.lat + '&lon=' + coords.lon + '&APPID='+key;


hindmost February 2016

That's because getCurrentPosition method is asynchronous. This mean that getCurrentPosition's callback is not invoked at the moment of calling changeAccess function. So you have to place changeAccess call into getCurrentPosition's callback:

function myLocation() {
    navigator.geolocation.getCurrentPosition(function(position) {
        coords.lat = (Math.round(position.coords.latitude*100)/100).toString();
        coords.lon = (Math.round(position.coords.longitude*100)/100).toString();
    });
    changeAccess(coords, apikey);
}


Thomas February 2016

Either

var apikey = '9575f3355ae129dc91424b5712a7695e';
var accessOWM;

function round(v){ return Math.round(v*100)/100 }

function myLocation(){
    navigator.geolocation.getCurrentPosition(function(position){
        changeAccess(position.coords);
    });
}

function changeAccess(coords){
    console.log(coordObj);
    accessOWM ='http://api.openweathermap.org/data/2.5/forecast?lat=' + round(coords.latitude) + '&lon=' + round(coords.longitude) + '&APPID=' + apikey;
    console.log(accessOWM);
}

myLocation();

Or

var apikey = '9575f3355ae129dc91424b5712a7695e';
var accessOWM = myLocation().then(changeAccess);

accessOWM.then(function(v){
    console.log(v);
})

function round(v){ return Math.round(v*100)/100 }

function myLocation(){
    return new Promise(function(resolve){
        navigator.geolocation.getCurrentPosition(function(position){
            resolve(position.coords);
        });
    });
}

function changeAccess(coords){
    return 'http://api.openweathermap.org/data/2.5/forecast?lat=' + round(coords.latitude) + '&lon=' + round(coords.longitude) + '&APPID=' + apikey;
}

Post Status

Asked in February 2016
Viewed 2,526 times
Voted 6
Answered 4 times

Search




Leave an answer