地图的返回值未定义

时间:2018-06-26 11:57:14

标签: javascript function ecmascript-6

我的数据看起来像这样:

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

问题是我无法返回正确的结果。当前返回undefined。 我觉得这可能与范围或函数的调用方式有关。

这是我到目前为止所拥有的:

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);

预期结果是:(假设用户无法访问管理员)

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

编辑:添加CheckInGroup函数

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);
        });
    });
}

1 个答案:

答案 0 :(得分:1)

.then()始终返回承诺,因此obj === enabled将不起作用。您不能使异步代码同步。一旦有了异步功能,链中的每个调用者都必须能够处理此问题。

您可以获得一份承诺清单,等到所有承诺都解决后再过滤掉无效的承诺:

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));
});