1

I want to store data like thispublic initData: any = ['Group 1', 'Group 2', 'Group 3'];. I am calling an API and It gives this response Image

I declare private initData: any; and after getting the response I try to store in that like this

this.userService.getUserRolesById(this.id)
    .subscribe(data => {
        this.initData = data.Name;
        console.log(this.initData);
})

userservice

public getUserRolesById(id: number) {
    return this.httpHelper.get(this._getUserRolesByIdUrl+'?userId='+id)
        .map(res => res.json());
}
2
  • Can you please add userService.getUserRolesById implementation? Commented Sep 15, 2016 at 12:31
  • code updated as requested Commented Sep 15, 2016 at 12:32

1 Answer 1

3

The problem here is that you get an array and you want a property in this array.

public initData:string[] = [];//Typing your arrays is important ;).
...
...
...
this.userService.getUserRolesById(this.id)
    .subscribe(roles => {
        roles.forEach(role => {
            console.log(role.Name);
            this.initData.push(role.Name);
        }
});
Sign up to request clarification or add additional context in comments.

2 Comments

It works but one thing more I want to store it like this public initData: any = ['Admin', 'Guest', 'Super Admin'];
I added a push to the initData field.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.