node + ES6 + Promises:插入嵌套循环时出错

时间:2017-06-29 17:03:39

标签: node.js es6-promise

当未插入嵌套循环时,以下Promise函数运行良好

export function addRolesInGroups(groups) {
  // role items setup
  const roleA1 = { _groupId: groups[0]._id, name: 'admin', description: 
  'can RW group, user', users: [], permissions: permissions1 };
  const roleA1 = ...;

  return Role.insertMany([roleA1, roleA2, roleA3, roleB2, roleB3, roleC3, role4 ])
    .then((insertedRoles) => {
      // nested loops removed
      console.log('NO ERROR');
      return groups;
    })
    .catch((e) => {
      console.log('ERROR IN role insert: %j', e);
      throw e;
    });
}

的console.log

NO ERROR

但是当插入嵌套循环时,任何消息都会收到错误 我的循环出了什么问题?

export function addRolesInGroups(groups) {
  // role items setup
  const roleA1 = ...;

  return Role.insertMany([roleA1, roleA2, roleA3, roleB2, roleB3, roleC3, role4 ])
    .then((insertedRoles) => {
      // nested loops inserted: will update groups with insertedRoles ids
      for (const role of insertedRoles) {
        for (const group of groups) {
          if (group._id === role._groupId.toString()) { group._roles.push(role._id); }
        };
      };
      console.log('NO ERROR');
      return groups;
    })
    .catch((e) => {
      console.log('ERROR IN role insert: %j', e);
      throw e;
    });
}

的console.log

ERROR IN role insert: {} 

已更新

的console.log

insertedRoles [{"__v":0,"_groupId":"5955382175bdef3c5713dd9e","name":"admin","description":"can RW group, user","_id":"5955382175bdef3c5713dda1","rolePermissions":[],"permissions":[{"resourceName":"group","authorizedActions":["read","write"]},{"resourceName":"user","authorizedActions":["read","write"]}],"users":[]},{"__v":0,"_groupId":"5955382175bdef3c5713dd9e","name":"manager","description":"can R group, can RW user","_id":"5955382175bdef3c5713dda2","rolePermissions":[],"permissions":[{"resourceName":"group","authorizedActions":["read"]},{"resourceName":"user","authorizedActions":["read","write"]}],"users":[]},{"__v":0,"_groupId":"5955382175bdef3c5713dd9e","name":"employee","description":"can R group, can read user","_id":"5955382175bdef3c5713dda3","rolePermissions":[],"permissions":[{"resourceName":"group","authorizedActions":["read"]},{"resourceName":"user","authorizedActions":["read"]}],"users":[]},{"__v":0,"_groupId":"5955382175bdef3c5713dd9f","name":"manager","description":"can R group, can RW user","_id":"5955382175bdef3c5713dda4","rolePermissions":[],"permissions":[{"resourceName":"group","authorizedActions":["read"]},{"resourceName":"user","authorizedActions":["read","write"]}],"users":[]},{"__v":0,"_groupId":"5955382175bdef3c5713dd9f","name":"employee","description":"can R group, can read user","_id":"5955382175bdef3c5713dda5","rolePermissions":[],"permissions":[{"resourceName":"group","authorizedActions":["read"]},{"resourceName":"user","authorizedActions":["read"]}],"users":[]},{"__v":0,"_groupId":"5955382175bdef3c5713dda0","name":"employee","description":"can R group, can read user","_id":"5955382175bdef3c5713dda6","rolePermissions":[],"permissions":[{"resourceName":"group","authorizedActions":["read"]},{"resourceName":"user","authorizedActions":["read"]}],"users":[]},{"__v":0,"name":"visitor","description":"no permissions","_id":"5955382175bdef3c5713dda7","rolePermissions":[],"permissions":[],"users":[]}]

更新2

  const insertedRolesIterable = insertedRoles => !!insertedRoles && typeof insertedRoles[Symbol.iterator] === 'function';
  const groupsIterable = groups => !!groups && typeof groups[Symbol.iterator] === 'function';
  console.log('insertedRoles is iterable ?', insertedRolesIterable());
  console.log('groups is iterable ?', groupsIterable());

0 个答案:

没有答案