关于类方法的内部调用好像不起作用
来源:11-3 动态菜单渲染实现(下)

慕雪9296518
2021-06-23
老师好,我用的是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)
我就搞不明白了
1回答
-
河畔一角
2021-06-24
能贴程序调用的完整代码吗? 你上面发的代码,我只看到UsersCTl下面定义了getMenuList,并没有看到你在哪儿调用的?
理论上getMenuList属于UsersCTI下面的方法,通过对象是可以访问到的。
012021-06-24
相似问题