I'm running into an issue with setting the value of global variables. When my function GetUserList() is called, it returns an array of globalUsers when it is run inside the function.
However, when I log the console again after running the function, the array being returned is empty. I feel there must be something wrong with the logic, or the manner in which the code is being executed.
I'm seeking that the value of globalUsers is set within the function for future use throughout the rest of the script.
Your assistance would be greatly appreciated. See the code below:
var globalUsers = [];
function GetUserList(){
var userList = $().SPServices.SPGetListItemsJson({
listName: "Users",
CAMLQuery: "<Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query>",
async: false,
mappingOverrides: {
ows_Title: {
mappedName: "Name",
objectType: "Text"
},
ows_ID: {
mappedName: "id",
objectType: "Text"
},
ows_Group: {
mappedName: "Group",
objectType: "Text"
}
}
});
$.when(userList).done(function() {
thisUserList = this.data;
globalUsers = thisUserList;
console.log(globalUsers);
});
}
$(document).ready(function () {
GetUserList();
console.log(globalUsers);
}
);
thisUserListanywhere? Also try toconsole.log(this.data)whether it returns something and what type