Looking to create a handler that generates an array of categories based on the presence of "categories" for a specific "resource". However, encountering an error with the last method. ERROR TypeError: "this.allProjectResources is undefined"
import { ResourceCategory } from './resourceCategory';
import { Injectable } from '@angular/core';
import { Resource } from './resource';
import { HttpService } from '../project/http.service';
import { isNullOrUndefined } from 'util';
@Injectable()
export class ResourceService {
public allProjectResources: Resource[];
public allResourceCategories: ResourceCategory[];
uniqueResCategories = new Set();
constructor(private http: HttpService) { }
getAllResourcesByProjId(projid: number) {
if (!(isNullOrUndefined(this.allProjectResources))) {
this.allProjectResources.length = 0;
}
this.http.getAllResourcesById(projid).subscribe((data: Resource[]) =>
this.allProjectResources = data);
}
getAllResourceCategories() {
this.http.getAllResourceCategories().subscribe((data: ResourceCategory[]) =>
this.allResourceCategories = data);
}
getAllUniqieResourceCategories(){
this.allProjectResources.forEach(resource => {
this.allResourceCategories.forEach(function (category) {
if (resource.resourceCategoryId == category.id) {
this.uniqueResCategories.add(category.name);
console.log(category.name);
};
});
});
}
}