Justin
Justin

Reputation: 972

Return value from map is undefined

I have data that looks something like this:

[ 
  {  ItemID: 1,  Path: '/Admin',     Name: 'Admin'   },
  {  ItemID: 2,  Path: '/Product',   Name: 'Product' },
  {  ItemID: 3,  Path: '/Reports',   Name: 'Reports' } 
]

The issue is I can't get it to return the right result. It currently returns undefined. I have a feeling it might be to do with scope or maybe how the function is called.

This is what I have so far:

const result = data
    .map(curr => {

        // Map over the object and check the names of the object
        // create a new key `groupName` if matched

        if (curr.Name == "Admin") {
            let groupName = "Admin Access";
            return { ...curr, groupName: groupName };
        } else if (curr.Name == "Product") {
            let groupName = "Product Access";
            return { ...curr, groupName: groupName };
        } else if (curr.Name == "Reports") {
            let groupName = "Reports";
            return { ...curr, groupName: groupName };
        }
    })
    .map(obj => {
// obj now looks like this
//{ ItemID: 1, Path: '/Admin',  Name: 'Admin',   groupName: 'Admin Access' }
//{ ItemID: 2, Path: '/Product',Name: 'Product', groupName: 'Product Access'}
//{ ItemID: 3, Path: '/Reports',Name: 'Reports', groupName: 'Reports' }

        // Map over the object and check permissions for access

        let enabled = checkInGroup(username, obj.groupName)
        .then(function(isMember) {
            if (isMember) {
                return obj; //isMember will return true or false
            }
        });
        return obj == enabled; //if enabled then add to return object
    });

console.log("===> result ", result);

The result expected is: (assuming a user is unable to access Admin)

[ 
  {  ItemID: 2,  Path: '/Product',   Name: 'Product' },
  {  ItemID: 1,  Path: '/Reports',   Name: 'Reports' } 
]

EDIT: ADD CheckInGroup Function

function checkInGroup(username, groupName) {
    return new Promise(function(resolve, reject) {
        ad.isUserMemberOf(username, groupName, function(err, isMember) {
            if (err) {
                return res.json("Error ", err);
            }
            resolve(isMember);
        });
    });
}

Upvotes: 0

Views: 139

Answers (1)

Felix Kling
Felix Kling

Reputation: 816334

.then() always returns a promise, so obj === enabled will not work. You cannot make asynchronous code synchronous. Once you have a function that is asynchronous, every caller up the chain needs to be able to handle this.

You can get a list of promises and wait until all of them are resolved and then filter out the ones that are not valid:

const promises = data
    .map(curr => {

        // Map over the object and check the names of the object
        // create a new key `groupName` if matched

        if (curr.Name == "Admin") {
            let groupName = "Admin Access";
            return { ...curr, groupName: groupName };
        } else if (curr.Name == "Product") {
            let groupName = "Product Access";
            return { ...curr, groupName: groupName };
        } else if (curr.Name == "Reports") {
            let groupName = "Reports";
            return { ...curr, groupName: groupName };
        }
    })
    .map(obj => {
        // Map over the object and check permissions for access
        return checkInGroup(username, obj.groupName)
        .then(function(isMember) {
            obj.enabled = isMember;
            return obj;
        });
    });

Promise.all(promises).then(result => {
  console.log(result.filter(obj => obj.enabled));
});

Upvotes: 1

Related Questions