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

0

I think the issue is not related to isMember because it's already defined outside of all functions so it's in the global scope. so make sure that isCurrentUserInGroup is already defined.

Meanwhile, To know which variable specifically is undefined try to use F12 developer tools to check the error in console tab.

Note: Do NOT create global variables unless you intend to.In "Strict Mode" automatically global variables will fail. for more details check JavaScript Scope

|improve this answer|||||
  • I have updated the code – HAJJAJ Apr 12 '17 at 21:59
0

U can use an auxiliar function

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

    function newFunction(param) {
        console.log(param)
    }

Can you remove that return true from the if. In fact, you should return just the isCurrentUserInGroup var, that var is already a bool. I test your code it work just fine: https://jsfiddle.net/wwh7eroa/

|improve this answer|||||
  • i got undefined – HAJJAJ Apr 12 '17 at 22:37
  • answer improved – Marco Apr 13 '17 at 12:27
0

You're attempting to do something fundamentally impossible: access a value in a synchronous context which is not yet available because it's set asynchronously. Have a look at my comments:

var isMember;

IsCurrentUserMemberOfGroup("Team Management System Members", function (isCurrentUserInGroup) {
    isMember = isCurrentUserInGroup; //Executed AFTER the alert
});

alert(isMember); //This line is executed BEFORE the the value is set (hence undefined)

Now, I think you need to understand this concept and re-write your code appropriately. Move your 'alert' - or whatever - into the function where isCurrentUserInGroup is available.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.