I'm using MEAN
Stack to build a grading app. I'm trying to get a list of items from the database into my Angular component via a service code but i keep getting core.js:14597 ERROR TypeError: Cannot read property 'map' of undefined
.
//scoring.service.js
import { Scoring } from './scoring.model';
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
@Injectable({providedIn: 'root'})
export class ScoringService {
private scoring: Scoring[] = [];
private updatedScores = new Subject<Scoring[]>();
constructor(private http: HttpClient, private router: Router){}
getScoring() {
this.http.get<{message: string, scores: any}>('http://localhost:3000/api/scoring')
.pipe(map((scoringData) => {
return scoringData.scores.map(score => {
status = score.IsActive ? 'checked' : 'unchecked';
return {
id: score._id,
Criteria: score.Criteria,
MaxPoints: score.MaxPoints,
IsActive: status,
DateCreated: score.DateCreated,
DateModified: score.DateModified
};
});
}))
.subscribe((transformedScores) => {
this.scoring = transformedScores;
this.updatedScores.next([...this.scoring]);
});
}
}
The code is supposed to list the items and map a boolean
field from true
or false
to checked or unchecked respectively. But nothing is being listed at all. The error i'm getting is "Cannot read property 'map' of undefined."
And I've used the same set of code in another component to list items without getting the error. Please, help me here, I will appreciate. Thanks.