Pushing getCurrentPosition() values into array but cannot console log elements of the array











up vote
0
down vote

favorite












I've wrapped the geolocation API in the getLocation() function and am returning an array. However, when I try to access the specific elements of the array, I am getting undefined. I feel like I'm missing something very simple here.



 const getLocation = function () {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});
return arrLocations;
}
const coord = getLocation();
console.log(coord);
console.log(coord[0]);


I've also tried to wrap the geolocation in a promise just in case there is some async happening with getCurrentPosition. The call returns undefined. (I'm not sure if I've written the promise right. I'm relatively new to JavaScript):



    new Promise(function (resolve, reject) {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});

if (!arrLocations) {
resolve(arrLocations);
}
else {
reject();
}
})
.then(function (arr) {
return arr;
})
.catch(function (e) {
console.log(`Something went wrong: ${e}`);
});


Why is the element in the array returning undefined? And why is the promise returning undefined? Thanks!










share|improve this question






















  • If you try to log arrLocations from within getLocation(), it returns empty too...So you might want to start from there
    – tera_789
    Nov 11 at 6:03












  • I'm not sure what you mean. I'm getting the coordinates as I expect them. I put a console.log before the return statement.
    – Jacob L
    Nov 11 at 6:14















up vote
0
down vote

favorite












I've wrapped the geolocation API in the getLocation() function and am returning an array. However, when I try to access the specific elements of the array, I am getting undefined. I feel like I'm missing something very simple here.



 const getLocation = function () {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});
return arrLocations;
}
const coord = getLocation();
console.log(coord);
console.log(coord[0]);


I've also tried to wrap the geolocation in a promise just in case there is some async happening with getCurrentPosition. The call returns undefined. (I'm not sure if I've written the promise right. I'm relatively new to JavaScript):



    new Promise(function (resolve, reject) {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});

if (!arrLocations) {
resolve(arrLocations);
}
else {
reject();
}
})
.then(function (arr) {
return arr;
})
.catch(function (e) {
console.log(`Something went wrong: ${e}`);
});


Why is the element in the array returning undefined? And why is the promise returning undefined? Thanks!










share|improve this question






















  • If you try to log arrLocations from within getLocation(), it returns empty too...So you might want to start from there
    – tera_789
    Nov 11 at 6:03












  • I'm not sure what you mean. I'm getting the coordinates as I expect them. I put a console.log before the return statement.
    – Jacob L
    Nov 11 at 6:14













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've wrapped the geolocation API in the getLocation() function and am returning an array. However, when I try to access the specific elements of the array, I am getting undefined. I feel like I'm missing something very simple here.



 const getLocation = function () {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});
return arrLocations;
}
const coord = getLocation();
console.log(coord);
console.log(coord[0]);


I've also tried to wrap the geolocation in a promise just in case there is some async happening with getCurrentPosition. The call returns undefined. (I'm not sure if I've written the promise right. I'm relatively new to JavaScript):



    new Promise(function (resolve, reject) {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});

if (!arrLocations) {
resolve(arrLocations);
}
else {
reject();
}
})
.then(function (arr) {
return arr;
})
.catch(function (e) {
console.log(`Something went wrong: ${e}`);
});


Why is the element in the array returning undefined? And why is the promise returning undefined? Thanks!










share|improve this question













I've wrapped the geolocation API in the getLocation() function and am returning an array. However, when I try to access the specific elements of the array, I am getting undefined. I feel like I'm missing something very simple here.



 const getLocation = function () {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});
return arrLocations;
}
const coord = getLocation();
console.log(coord);
console.log(coord[0]);


I've also tried to wrap the geolocation in a promise just in case there is some async happening with getCurrentPosition. The call returns undefined. (I'm not sure if I've written the promise right. I'm relatively new to JavaScript):



    new Promise(function (resolve, reject) {
const arrLocations = ;
navigator.geolocation.getCurrentPosition(function (position) {
arrLocations.push(position.coords.latitude)
arrLocations.push(position.coords.longitude)
});

if (!arrLocations) {
resolve(arrLocations);
}
else {
reject();
}
})
.then(function (arr) {
return arr;
})
.catch(function (e) {
console.log(`Something went wrong: ${e}`);
});


Why is the element in the array returning undefined? And why is the promise returning undefined? Thanks!







javascript geolocation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 5:59









Jacob L

33




33












  • If you try to log arrLocations from within getLocation(), it returns empty too...So you might want to start from there
    – tera_789
    Nov 11 at 6:03












  • I'm not sure what you mean. I'm getting the coordinates as I expect them. I put a console.log before the return statement.
    – Jacob L
    Nov 11 at 6:14


















  • If you try to log arrLocations from within getLocation(), it returns empty too...So you might want to start from there
    – tera_789
    Nov 11 at 6:03












  • I'm not sure what you mean. I'm getting the coordinates as I expect them. I put a console.log before the return statement.
    – Jacob L
    Nov 11 at 6:14
















If you try to log arrLocations from within getLocation(), it returns empty too...So you might want to start from there
– tera_789
Nov 11 at 6:03






If you try to log arrLocations from within getLocation(), it returns empty too...So you might want to start from there
– tera_789
Nov 11 at 6:03














I'm not sure what you mean. I'm getting the coordinates as I expect them. I put a console.log before the return statement.
– Jacob L
Nov 11 at 6:14




I'm not sure what you mean. I'm getting the coordinates as I expect them. I put a console.log before the return statement.
– Jacob L
Nov 11 at 6:14












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










getCurrentPosition() is asynchronous, which is why your first snippet doesn't work. You are returning and trying to log arrLocations before the async function has pushed anything. Using the promise in your second idea is a good intuition, it just needs a little tweaking.



Here's one way. Just resolve the array you want and take advantage of getCurrentPosition's second parameter for an error call back to reject if needed. (you'll probably just get the error in a SO snippet):






const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}

// to use it:

getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))








share|improve this answer





















  • That worked. Thanks!
    – Jacob L
    Nov 11 at 13:00











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246252%2fpushing-getcurrentposition-values-into-array-but-cannot-console-log-elements-o%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










getCurrentPosition() is asynchronous, which is why your first snippet doesn't work. You are returning and trying to log arrLocations before the async function has pushed anything. Using the promise in your second idea is a good intuition, it just needs a little tweaking.



Here's one way. Just resolve the array you want and take advantage of getCurrentPosition's second parameter for an error call back to reject if needed. (you'll probably just get the error in a SO snippet):






const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}

// to use it:

getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))








share|improve this answer





















  • That worked. Thanks!
    – Jacob L
    Nov 11 at 13:00















up vote
0
down vote



accepted










getCurrentPosition() is asynchronous, which is why your first snippet doesn't work. You are returning and trying to log arrLocations before the async function has pushed anything. Using the promise in your second idea is a good intuition, it just needs a little tweaking.



Here's one way. Just resolve the array you want and take advantage of getCurrentPosition's second parameter for an error call back to reject if needed. (you'll probably just get the error in a SO snippet):






const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}

// to use it:

getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))








share|improve this answer





















  • That worked. Thanks!
    – Jacob L
    Nov 11 at 13:00













up vote
0
down vote



accepted







up vote
0
down vote



accepted






getCurrentPosition() is asynchronous, which is why your first snippet doesn't work. You are returning and trying to log arrLocations before the async function has pushed anything. Using the promise in your second idea is a good intuition, it just needs a little tweaking.



Here's one way. Just resolve the array you want and take advantage of getCurrentPosition's second parameter for an error call back to reject if needed. (you'll probably just get the error in a SO snippet):






const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}

// to use it:

getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))








share|improve this answer












getCurrentPosition() is asynchronous, which is why your first snippet doesn't work. You are returning and trying to log arrLocations before the async function has pushed anything. Using the promise in your second idea is a good intuition, it just needs a little tweaking.



Here's one way. Just resolve the array you want and take advantage of getCurrentPosition's second parameter for an error call back to reject if needed. (you'll probably just get the error in a SO snippet):






const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}

// to use it:

getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))








const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}

// to use it:

getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))





const getLocation = function() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve([position.coords.latitude, position.coords.longitude]),
(error) => reject(error)
);
})
}

// to use it:

getLocation()
.then(arrLocations => console.log(arrLocations))
.catch(err => console.log("there was an error: ", err))






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 6:15









Mark Meyer

30.4k32550




30.4k32550












  • That worked. Thanks!
    – Jacob L
    Nov 11 at 13:00


















  • That worked. Thanks!
    – Jacob L
    Nov 11 at 13:00
















That worked. Thanks!
– Jacob L
Nov 11 at 13:00




That worked. Thanks!
– Jacob L
Nov 11 at 13:00


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246252%2fpushing-getcurrentposition-values-into-array-but-cannot-console-log-elements-o%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation