Node JS Javascript类TypeError不是函数

时间:2017-10-13 13:06:51

标签: javascript node.js sequelize.js

我无法理解为什么我的代码无效。我有一个基本类,有两个类方法。我试图从第一个方法中访问第二个类方法,并得到一个类型错误,说第二个方法不是一个函数。大声笑我一直这样做,我错过了什么?

import models from '../../db';

class GroupRoleCheck {

  constructor() {
    this.group = null;
  }

  isGroupAdmin(params, callback) {
    let groupId = params.groupId;
    let userId = params.user.id;

    models.Group.findById(groupId, {
      include: [{ all: true }]
    })
    .then(group => {
      this.group = group;

      let found = this.group.Users.find(user => {
        return userId === user.id;
      });

      if (found) {
        return found.GroupMembers;
      }
    })
    .then(members => {

      if (members.role === 'groupAdmin') {
        return callback(null, true);
      } else {

        this.isOrgSuper();

        return callback({
          message: 'not authorized'
        }, false)
      }
    })
  }

  isOrgSuper() {
    console.log('===================================');
    console.log(this.group);
    console.log('===================================');
  }
}

export default GroupRoleCheck;

enter image description here

enter image description here

更新

我从另一个文件中调用它,我正在实现RBAC2并传递调用rbac。

import RBAC from 'rbac2';

import UserRoleCheck from '../permissions/user.permission';
import OrgRoleCheck from '../permissions/org.permission';
import GroupRoleCheck from '../permissions/group.permission';

let userRoleCheck = new UserRoleCheck();
let orgRoleCheck = new OrgRoleCheck();
let groupRoleCheck = new GroupRoleCheck();

let rules = [
  // Users:
  {
    a: 'admin',
    can: 'view organizations',
    when: userRoleCheck.isOrgAdminAll
  },
  // Organizations:
  {
    a: 'admin',
    can: 'view org details',
    when: orgRoleCheck.isOrgAdmin
  },
  {
    a: 'super',
    can: 'edit org details',
    when: orgRoleCheck.isOrgSuper
  },
  {
    a: 'super',
    can: 'delete org',
    when: orgRoleCheck.isOrgSuper
  },
  // Groups:
  {
    a: 'admin',
    can: 'create group',
    when: orgRoleCheck.isOrgAdmin
  },
  {
    a: 'admin',
    can: 'view group details',
    when: groupRoleCheck.isGroupAdmin
  },
  {
    a: 'super',
    can: 'edit group details',
    when: orgRoleCheck.isOrgSuper
  },
  {
    a: 'super',
    can: 'delete group',
    when: orgRoleCheck.isOrgSuper
  }
];

let rbac = new RBAC(rules);
export default rbac;

1 个答案:

答案 0 :(得分:0)

这是一个上下文问题,通过引用您的isGroupAdmin函数,您将失去对实例的跟踪。我假设RBAC内部将通过callapply调用该函数。

简单修复是以when调用实例上的函数的方式更新规则,例如when: (params, cb) => orgRoleCheck.isGroupAdmin(params, cb)