0

I am trying to build a baseline JS file to include in my some of my pages to return information from my SharePoint site. I would like to make three ajax calls to the server for the following: siteGroups, siteRoles and currentUser information and build an options array that I can use later by accessing it using dot notation. (e.g. options.siteGroup, options.siteRoles, options.currentUser.UserName, etc)

var options = (function(){
    var currentUser = (function(){ 
        var userInfo = [];
        function complete(data){
            for(i = 0; i < data.d.Groups.results.length; i++){
                var user = {groupId : data.d.results[i].groupId, groupName : 
           data.d.results[i].groupName, UserName : data.d.UserName, UserId : 
           data.d.UserId};
                UserInfo.push(user);
            }
        };
        getCurrentUser(url, query, complete);
        return userInfo;
    })();
    // Get All Site Permission Groups
    var siteGroups = (function(){
        function complete(data){
            var siteGroups = [];
            for(j = 0; j < data.d.results.length; j++){
                var group = {groupName : data.d.results[j].Title, groupId : 
                            data.d.results[j].Id};
                siteGroups.push(group);
            };
            $('.SiteGroups').append(option);
        };
        getSiteGroups(url, complete);
        return siteGroups;
    })();
    // Get All Role Definitions to be used for role assignment
    var siteRoles = (function(){
        var roles = [];
        function complete(data){
            var option;
            for(s = 0; s < data.d.results.length; s++){
               var role = {roleId : data.d.results[s].Id, roleName : 
               data.d.results[s].Name};
               roles.push(role);
            }
        }
        getRoleDefinitions(url, complete);
        return roles;
    })();
    return {currentUser, siteRoles, siteGroups};
})();

When I console.log(options); I get an array with the correct values, however every combination (options.currentUser, options.currentUser.userName, etc) has yielded either userName is undefined or currentUser[0] cannot find value at position 0. Appreciate the help in advance!

https://i.stack.imgur.com/OD7os.jpg "Image of console.log"

  • You're not returning anything from the options function enclosure are you? Try putting `return { siteRoles, siteGroups, currentUser }` right before the end of the most outer function. As you can see I use an Object instead of an array. – Khauri Jul 20 '17 at 15:22
  • I agree. I apologize I left that part out. I had it in there. I added a image of the response and how I am currently accessing it – Adam Kramer Jul 20 '17 at 15:40
  • I still have not figured this out though. Hopefully my first comment wasn't confusing. – Adam Kramer Jul 20 '17 at 20:24
  • It seems like you're calling asynchronous js code in order to populate a couple arrays, but you're trying to access elements in the array before the request is complete. You need a way to track when all the data is loaded. (A simple counter could work). Though maybe reconsider your approach to the problem and if you really need all the data loaded all at once. Just a suggestion. – Khauri Jul 20 '17 at 22:04
  • I believe the data is loaded before I am calling it. Or at least when expanding the object all the data I pushed to it is there as I intended. The problem is accessing it. The picture display what I am getting back from console.log(siteData) and console.log(siteData.currentUser) = [] > [0]:groupId : 123, [1]: groupName: Adam. The problem is going one step further and saying console.log(siteData.currentUser[0].groupName) that returns 'groupName' is not a defined variable – Adam Kramer Jul 20 '17 at 22:56
  • Yes, I know. Look where you're logging the data. It says `Array [ ]` instead of `Array [ 4 ]` or some other number. That means when you initally logged the data the array was empty so there is no `currentUser[0]`. However when you check the console later on the data is finally loaded. Try using `JSON.stringify`to log the array as a string. The console doesn't log the array as it was when you logged it, but rather always gets an up to date version of the array, if that makes sense. – Khauri Jul 20 '17 at 23:06
  • This explained the solution. Works now. https://stackoverflow.com/questions/2917175/return-multiple-values-in-javascript – Adam Kramer Jul 29 '17 at 02:14

0 Answers0