I am creating a project which uses a HTTP get from a web service and returns an array of projects, with ID, name, description etc.
There is many projects within this web service but I am only concerned with 9 of them the rest are irrelevant.
I have a desired set of 9 projects with unique ID's declared in the project.service.http.ts class that I want to only be showing.
I am trying to filter the HTTP get request to only include the 9 specific Ids, which I store in a projectIds array of type string.
EDIT2: With logging the response:
EDIT SOLVED: I changed the constructor in the project.viewer.component from :
constructor(private service: ProjectService) {
this.service.fetchProjects().subscribe(response => {
this.projects = response.filter(elements => {
// BaseComponent was my class. Use yours.
return ProjectViewerComponent.projectIds.includes(elements.id);
});
})
}
to :
constructor(private service: ProjectService) {
this.service.fetchProjects().subscribe(response => {
this.projects = response['project'].filter(elements => {
// BaseComponent was my class. Use yours.
return ProjectViewerComponent.projectIds.includes(elements.id);
});
})
}
The Key was the ['project'] after the this.projects = response
project.service.http.ts:
@Injectable()
export class ProjectServiceHttp extends ProjectService {
//variables
baseUrl = "http://...";
static projectIds: string[] = ["..."
,"...", "..","..."];
//constructor
constructor(private http: Http) {
super();
}
//methods
fetchProjects(): Observable<any>{
let headers = new Headers({'Content-Type': 'application/json'});
let options = new RequestOptions({headers: headers});
return this.http.get(this.baseUrl, options)
.map((response: Response) => response.json())
.catch(this.handleError);
}
private handleError(error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.log(errMsg); // log to console instead
return Observable.throw(errMsg);
}
}
project.viewer.component.ts:
@Component({
selector: 'project-viewer',
templateUrl: './project-viewer.html',
styleUrls: ['./project-viewer.css']
})
export class ProjectViewerComponent {
name = 'ProjectViewerComponent';
projects: Project[] = [];
static testIds: string[] = ['qqq', 'aaa'];
static projectIds: string[] = ["...","..."
,"..","..","...","..."
,"..", "...","..."];
errorMessage = "";
stateValid = true;
constructor(private service: ProjectService) {
this.service.fetchProjects().subscribe(response => {
this.projects = response.filter(elements => {
return ProjectViewerComponent.projectIds.includes(elements.id);
});
})
}
private raiseError(text: string): void {
this.stateValid = false;
this.errorMessage = text;
}
}
project-viewer.html:
<h3>Projects </h3>
<div >
<ul class= "grid grid-pad">
<a *ngFor="let project of projects" class="col-1-4">
<li class ="module project" >
<h4 tabindex ="0">{{project.name}}</h4>
</li>
</a>
</ul>
</div>