Encountering an issue when calling the groupSkillsByExpertise() function in ngOninit as the console.log displays this.memberSkills as undefined. However, the content of membersSkills can be displayed in the HTML template. The aim is to filter the array of memberSkills in the groupSkillsByExpertise() function to only show skills with expert level. Since memberSkills is undefined, the filter cannot be applied.
Below is the code from my member-profile-component.ts:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { MemberContact } from 'src/app/models/member-contact/member-contact.interface';
import { MemberProfileService } from 'src/app/services/member-profile/member-profile.service';
import { Skill } from 'src/app/models/skill/skill.interface';
import { Language } from 'src/app/models/language/language.model';
@Component({
selector: 'app-member-profile',
templateUrl: './member-profile.component.html',
styleUrls: ['./member-profile.component.css']
})
export class MemberProfileComponent implements OnInit {
memberId: number;
member: MemberContact;
memberSkills: Array<Skill>;
memberLanguages: Array<Language>;
expertList: Array<Skill>;
intermediateList: Array<Skill>;
beginnerList: Array<Skill>;
constructor(
private activatedRoute: ActivatedRoute,
private memberProfileService: MemberProfileService
) { }
ngOnInit(): void {
this.getMemberProfile();
this.getMemberSkills();
this.getMemberLanguages();
this.groupSkillsByExpertise();
}
getMemberProfile() {
this.memberId = Number(this.activatedRoute.snapshot.paramMap.get('id'));
this.memberProfileService.findMemberById(this.memberId)
.subscribe(member => this.member = member);
}
getMemberSkills() {
this.memberId = Number(this.activatedRoute.snapshot.paramMap.get('id'));
this.memberProfileService.findSkillsByMemberId(this.memberId)
.subscribe(skills => this.memberSkills = skills);
}
getMemberLanguages() {
this.memberId = Number(this.activatedRoute.snapshot.paramMap.get('id'));
this.memberProfileService.findLanguageByMemberId(this.memberId)
.subscribe(languages => this.memberLanguages = languages);
}
groupSkillsByExpertise() {
console.log(this.memberSkills); // Why is this undefined?
/* this.expertList = this.memberSkills.filter(skill => skill.skillLevel?.skillLevel === "Expert"); */
}
}