flipped-aurora/gin-vue-admin · error
找不到默认路由,无法切换本角色
Error message
找不到默认路由,无法切换本角色
What it means
When switching a user's active authority, SetUserAuthority checks that the target role's DefaultRouter is among that role's menus (authorityMenus). If no menu name matches authority.DefaultRouter, it returns errors.New("找不到默认路由,无法切换本角色"), because after switching, the user would land on a route they cannot access.
Source
Thrown at server/service/system/sys_user.go:186
for i := range authorityMenu {
authorityMenuIDs = append(authorityMenuIDs, authorityMenu[i].MenuId)
}
var authorityMenus []system.SysBaseMenu
err = global.GVA_DB.WithContext(ctx).Preload("Parameters").Where("id in (?)", authorityMenuIDs).Find(&authorityMenus).Error
if err != nil {
return err
}
hasMenu := false
for i := range authorityMenus {
if authorityMenus[i].Name == authority.DefaultRouter {
hasMenu = true
break
}
}
if !hasMenu {
return errors.New("找不到默认路由,无法切换本角色")
}
err = global.GVA_DB.WithContext(ctx).Model(&system.SysUser{}).Where("id = ?", id).Update("authority_id", authorityId).Error
return err
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: SetUserAuthorities
//@description: 设置一个用户的权限
//@param: id uint, authorityIds []string
//@return: err error
func (userService *UserService) SetUserAuthorities(ctx context.Context, adminAuthorityID, id uint, authorityIds []uint) (err error) {
return global.GVA_DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var user system.SysUser
TxErr := tx.Where("id = ?", id).First(&user).Error
if TxErr != nil {
logger.WithCtx(ctx).Mod("biz").Debug(TxErr.Error())View on GitHub (pinned to 3136500ef3)
Solutions
- Open role management and assign the menu matching DefaultRouter to the target authority.
- Or edit the authority's DefaultRouter to the Name of one of its assigned menus.
- Verify menu Name (route name) matches DefaultRouter exactly — case-sensitive, no leading slashes.
- Re-run menu/authority initialization data if the role's menu relations were seeded incorrectly.
Example fix
// before authority.DefaultRouter = "dashboard" // no menu named "dashboard" assigned // after authority.DefaultRouter = assignedMenus[0].Name // e.g. an actually assigned menu name
Defensive patterns
Strategy: validation
Validate before calling
// client pre-check before switching
const target = authorities.find(a => a.authorityId === id)
if (!target.menus.some(m => m.name === target.defaultRouter)) {
ElMessage.warning('该角色未配置默认路由菜单')
} Try / catch
try {
await setUserAuthority({ id, authorityId })
} catch (e) {
if (e.message.includes('找不到默认路由')) ElMessage.error('请先为该角色设置默认路由并分配对应菜单')
else throw e
} Prevention
- When creating a role, always assign at least one menu and set DefaultRouter to its exact Name.
- Keep DefaultRouter in sync whenever menus are renamed.
- Add a role-save validation that DefaultRouter exists in the assigned menu list.
When it happens
Trigger: Calling SetUserAuthority (sys_user.go:186) with a role whose DefaultRouter field names a menu that is not assigned to the role (missing from sys_authority_menus), or where the menu name does not exactly equal DefaultRouter.
Common situations: Admin creating a new role but forgetting to set a default router or not assigning the default-route menu to the role; renaming a menu without updating DefaultRouter; copying a role but not its menus.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/acc9d8b54deef0db.
Report an issue: GitHub.