stackdisplay
stackdisplay

Reputation: 2045

Child's object undefined in sample code

I am calling my chain promise function in such a way. But encounter problem XXX is not defined as follow. How can I solve it?

Note that console.log(res.coords.latitude) does return value but the function throws undefined

function getGPSLocation(geolocationOptions) {
    
}

function initMap(lat, lng) {
	console.log(lat);
	console.log(lng);
}

var initMap = function (res) {
    console.log(res.coords.latitude); // res.coords.latitude has value
    initMap(res.coords.latitude, res.coords.longitude); 
// Uncaught TypeError: Cannot read property 'latitude' of undefined

    return res;
};

getGPSLocation()
    .then(initMap);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Upvotes: 0

Views: 50

Answers (1)

Praveen Prasannan
Praveen Prasannan

Reputation: 7123

function initMapTest(lat, lng) {
    console.log(lat);
    console.log(lng);
}

var initMap = function (res) {
    console.log(res.coords.latitude); // res.coords.latitude has value
    initMapTest(res.coords.latitude, res.coords.longitude);

    return res;
};

Upvotes: 1

Related Questions