请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

关于类方法的内部调用好像不起作用

老师好,我用的是controller和model,router的文件结构,因此我先在controller中声明了一个类包含了两个方法,再new一个类出来,这里有个问题就是我类中函数相互引用按道理用this指向就可以,但是这里控制台报错了,说getMenuList方法识别不出,也就是this指向无效,能问问怎么解决吗

class UsersCTl {
async getPermissionList(ctx){
const token = ctx.header.authorization
let userInfo = util.decoded(token)
let menuList = getMenuList(userInfo.charater, userInfo.roleList);
ctx.body = util.success({ menuList });
}

async getMenuList(charater, roleKeys) {
    let rootList = []
    if (charater == 0) {
        rootList = await Menu.find({}) || []
    } else {
        // 根据用户拥有的角色,获取权限列表
        // 现查找用户对应的角色有哪些
        let roleList = await Role.find({ _id: { $in: roleKeys } })
        let permissionList = []
        roleList.map(role => {
        let { checkedKeys, halfCheckedKeys } = role.permissionList;
        permissionList = permissionList.concat([...checkedKeys, ...halfCheckedKeys])
        })
        permissionList = [...new Set(permissionList)]
        rootList = await Menu.find({ _id: { $in: permissionList } })
    }
    return util.getTreeMenu(rootList, null, [])
}
}

module.exports = new UsersCTl()

没有侦测到错误抛出
第一次经过jwt
errohandler检测到错误
err.message是Cannot read property ‘getMenuList’ of undefined
err.status是undefined
ctx.status是404
[2021-06-23T09:44:07.346] [ERROR] error - /users/getPermissionList GET 404 TypeError: Cannot read property ‘getMenuList’ of undefined
at getPermissionList (D:\项目\服务端\test\controllers/users.js:145:29)
at dispatch (D:\项目\服务端\test\node_modules\koa-compose\index.js:42:32)
at jwt (D:\项目\服务端\test\node_modules\koa-jwt\lib\index.js:73:16)
at auth (D:\项目\服务端\test\controllers/jwt.js:23:9)
at errorhandler (D:\项目\服务端\test\common/ErrorHandle.js:11:9)
at D:\项目\服务端\test\node_modules\koa-log4\koa-logger.js:86:7
at D:\项目\服务端\test/app.js:70:3
at serve (D:\项目\服务端\test\node_modules\koa-static\index.js:53:9)
at logger (D:\项目\服务端\test\node_modules\koa-logger\index.js:67:7)
at verifyParam (D:\项目\服务端\test\node_modules\koa-parameter\index.js:52:7)
at logger1 (D:\项目\服务端\test\common/Logger.js:7:5)
at cors (D:\项目\服务端\test\node_modules\koa2-cors\dist\index.js:91:9)
at serve (D:\项目\服务端\test\node_modules\koa-static\index.js:53:9)
我就搞不明白了

正在回答 回答被采纳积分+3

1回答

河畔一角 2021-06-24 09:35:27

能贴程序调用的完整代码吗? 你上面发的代码,我只看到UsersCTl下面定义了getMenuList,并没有看到你在哪儿调用的?

理论上getMenuList属于UsersCTI下面的方法,通过对象是可以访问到的。

0 回复 有任何疑惑可以回复我~
  • 提问者 慕雪9296518 #1
    class UsersCTl {
      async getPermissionList(ctx){
      const token = ctx.header.authorization
      let userInfo = util.decoded(token)
      let menuList = getMenuList(userInfo.charater, userInfo.roleList);
      ctx.body = util.success({ menuList });
    
       async getMenuList(charater, roleKeys) {
           let rootList = []
        if (charater == 0) {
            rootList = await Menu.find({}) || []
        } else {
            // 根据用户拥有的角色,获取权限列表
            // 现查找用户对应的角色有哪些
            let roleList = await Role.find({ _id: { $in: roleKeys } })
            let permissionList = []
            roleList.map(role => {
            let { checkedKeys, halfCheckedKeys } = role.permissionList;
            permissionList = permissionList.concat([...checkedKeys, ...halfCheckedKeys])
            })
            permissionList = [...new Set(permissionList)]
            rootList = await Menu.find({ _id: { $in: permissionList } })
        }
        return util.getTreeMenu(rootList, null, [])
       }
    }
    这样看的到吗???我是都把方法写到类中的,但是内部相互调用时候,用this访问getMenuList却是显示Cannot read property ‘getMenuList’ of undefined
    回复 有任何疑惑可以回复我~ 2021-06-24 09:49:37
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号