I'm a little confused as to why this isn't working. I'm guessing it's a scope issue but not entirely sure.
I get values pushed to myProjectIds
if I log inside the function. But outside I cannot figure out why the global array declared at the top is empty.
Appreciate any help!
var myProjects = [];
var myProjectIds = [];
function getProjects() {
$.ajax({
method: 'GET',
url: 'http://localhost:8080/project',
data: {
user: userId,
},
// dataType: "json",
beforeSend: function (xhr) {
/* Authorization header */
xhr.setRequestHeader("Authorization", "Bearer " + userToken);
},
success: function (response) {
myProjects = response;
for (var i = 0; i < myProjects.projects.length; i++) {
myProjectIds.push(myProjects.projects[i]._id);
}
}
})
}
getProjects();
console.log(myProjectIds)