-3

I am trying to get value out of java script function to a variable

var isMember;

                        IsCurrentUserMemberOfGroup("Team Management System Members", function (isCurrentUserInGroup) {
                            if (isCurrentUserInGroup) {
                                //alert("Admin");
                                isMember = true;
                                return true;
                            }
                            else {
                                isMember = false;
                                //alert("NoAdmin")
                            }
                        });

                        alert(isMember);

the other function used to be called

 function IsCurrentUserMemberOfGroup(groupName, OnComplete) {

                        var currentContext = new SP.ClientContext.get_current();
                        var currentWeb = currentContext.get_web();

                        var currentUser = currentContext.get_web().get_currentUser();
                        currentContext.load(currentUser);

                        var allGroups = currentWeb.get_siteGroups();
                        currentContext.load(allGroups);

                        var group = allGroups.getByName(groupName);
                        currentContext.load(group);

                        var groupUsers = group.get_users();
                        currentContext.load(groupUsers);

                        currentContext.executeQueryAsync(OnSuccess, OnFailure);

                        function OnSuccess(sender, args) {
                            var userInGroup = false;
                            var groupUserEnumerator = groupUsers.getEnumerator();
                            while (groupUserEnumerator.moveNext()) {
                                var groupUser = groupUserEnumerator.get_current();
                                if (groupUser.get_id() == currentUser.get_id()) {
                                    userInGroup = true;
                                    break;
                                }
                            }
                            OnComplete(userInGroup);
                        }

                        function OnFailure(sender, args) {
                            OnComplete(false);
                        }
                    }

but when I execute i am getting the value : undefined

thanks

HAJJAJ
  • 3,667
  • 14
  • 42
  • 70

2 Answers2

2

You have to ensure that the function associated with IsCurrentUserMemberOfGroup gets invoked before you attempt to check the value of isMember. You can't just put an alert() after the function's code and expect that it will run the function first.

Here's a working example:

var isMember;

function checkUser(isCurrentUserInGroup) {
  if (isCurrentUserInGroup) {
    isMember = true;
  } else {
    isMember = false;
  }
}

alert(isMember);  // undefined because function hasn't bee called yet

checkUser();      // invoke the function with no data being passed in

alert(isMember);  // false because undefined was passed into function

checkUser("Something");  // invoke the function with data being passed in

alert(isMember);  // true because something was passed into function
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

most likely there's some asynchronous stuff going inside IsCurrentUserMemberOfGroup, the callback you're passing will be postponed as soon as your async stuff completes.

Potentially your IsCurrentUserMemberOfGroup function could look like this this:

function IsCurrentUserMemberOfGroup(someString, callback){
  .. 
  //async stuff here
  myAPI.asyncMethod(callback);

  //here the parser will go on with the synchronous code and park in the event queue the callback for the async method
  ..
}

as soon as the parser encounter the async function, the callback will be parked inside the event queue. the async function, (e.g. reading from file, deal with the network, etc.) will be handled by some other OS task that will notify the javascript thread when they finish, so that the callback could be finally invoked.

The problem is here. the parser executes synchronously, line by line, so it must keep going to not block the ui, that's why it reach the alert function before the async stuff completes, and print undefined, because member is still not assigned.

Karim
  • 8,454
  • 3
  • 25
  • 33