简体   繁体   English

Public get从auth服务返回一个权限布尔值,该权限布尔值初始化为false,并在NGRX存储返回值但未定义时进行修改

[英]Public get returns a permission boolean from auth service, which is initialized to false and modified when NGRX store returns a value but is UNDEFINED

I have an Angular7 app and I'm choosing to display a button in app.component based on a boolean that's defined as: 我有一个Angular7应用程序,我选择根据定义为以下内容的布尔值在app.component中显示一个按钮:

get superUser(): boolean {
    return this.auth.userPermissions.SUPER_USER;
  }

Now in my auth service, I have the userPermissions initialized after the Permissions model with all permissions marked as 'false'. 现在,在我的身份验证服务中,我在Permissions模型之后初始化了userPermissions,并将所有权限标记为'false'。

permissions: Permissions = {
    SUPER_USER: false,
    CLIENT_ADMIN: true,
    ADD_CLIENT: false,
    ADD_USER: false,
    DELETE_CLIENT: false,
    DELETE_USER: false,
    READ_ALL_CLIENTS: false,
    READ_ALL_USERS: false,
    READ_REPORTS: false,
    READ_USERS: false,
    UPDATE_CLIENT: false
  };

  public get userPermissions(): Permissions {
    return this.permissions;
  }

On Authentication, the permissions array is sent to the reducer and permissions are being set with ngrx store. 在Authentication上,将权限数组发送到reducer,并使用ngrx store设置权限。

.subscribe((token: string) => {
            Cookie.set('access_token', token);
            this.store.dispatch(new UserActions.SetUser(this.currentUser));
            const currentUser = this.helper.decodeToken(token);
            this.store.dispatch(
              new PermissionsActions.SetPermissions(currentUser.permissions)
            );
            return this.router.navigate(['']);
          });

In the service, I'm subscribing to that store value and updating the permissions object. 在该服务中,我正在订阅该存储值并更新权限对象。

    this.permissionSubscription = this.store
      .select('permissions')
      .subscribe(permissions => {
        this.permissions = permissions;
      });

Now the trouble is, the very first time after authentication the whole thing crashes returning: 现在的麻烦是,认证之后的第一次,整个事情崩溃了:

Cannot read property 'SUPER_USER' of undefined

Referencing the line where I defined the getter. 引用定义吸气剂的那一行。

But it is defined everywhere. 但是到处都有定义。 There's not a place in my code I haven't initialized and defined the model variable with the 'false' default. 我的代码中没有一个地方,我尚未初始化并使用默认值“ false”定义了模型变量。

In my reducer that processes the array of permissions: 在我的reducer中处理权限数组:

  const keys: string[] = Object.keys(state);

  for (const permission of action.permissions) {
    if (keys.includes(permission.name)) {
      state[permission.name] = true;
    } else {
      return console.log('ELSE BLOCK FIRING');
    }
  }

  return tassign(state);

It seems that I forgot to update the model with permissions that were in that array and when the for loop got to that, it made the whole state undefined. 似乎我忘记了使用该数组中的权限来更新模型,并且当for循环达到该权限时,整个状态变得不确定。

最终变成了reducer shenanigans,其中一个函数试图使用不存在的属性来做某事,每次都将init上的整个对象弄乱了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM