2

I need to set array property in angular 6 with this code :

this.addupdate.roleids=this.selectedRole;

but it shows me this error :

ERROR TypeError: Cannot set property 'roleids' of undefined at AccessLevelComponent.push../src/app/admin/admin/dashboard/role/access-level/access-level.component.ts.AccessLevelComponent.AddRoleClaim (access-level.component.ts:60)

selectedRole:string[]=['1011','1010','1005'];

and my interface :

export interface IAddorupdateRole {
  roleids:string[];
  roleid:number;
}

this my code :

public AddRoleClaim(){
  console.log("enter AddRoleClaim.Ts");
  this.addupdate.roleids=this.selectedRole;
  this.addupdate.roleid=this.roleId;
  this.roleService.AddOrUpdateRoleCalim(this.addupdate).subscribe((data)=>
    {
      console.log("seccess" + data);
    }
  );
}

What's the problem ? how can I solve that?

1 Answer 1

2

roleids is to be accessed on this.addupdate but apparently this.addupdate is undefined. Maybe it hasn't been initialized yet.

Try to initialize this.addupdate to a basic object in the ngOnInit something like this:

addupdate: IAddorupdateRole;

ngOnInit() {
  this.addupdate = {
    roleids = [],
    roleid = 0
  }
}

Here's a Sample StackBlitz for your ref.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.