Sharepoint - SharePoint 2013 get user groups by CSOM

Move this line

var groups = currentUser.get_groups();

To your getCurrentUser method and make sure to load it

this.clientContext.load(groups);

before executing the request, this way you will have it loaded in your success-function


The code should look like this:

    function getCurrentUser() {
        this.clientContext = new SP.ClientContext.get_current();
        this.oWeb = clientContext.get_web();
        currentUser = this.oWeb.get_currentUser();
        this.allGroups = currentUser.get_groups();    
        clientContext.load(allGroups);
        this.clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededUser), Function.createDelegate(this, this.onQueryFailedUser));
    }
    function onQuerySucceededUser() {
        var groupsEnum = allGroups.getEnumerator();
        while (groupsEnum.moveNext()) {

        //handle group
        var group = groupsEnum.get_current();
        }
    }