While attempting to implement the function getProblems
to retrieve all problems within its array, I encountered an error message appearing on res.json()
stating:
Promise is not assignable to parameters of type Problem[].
It seems that the function is treating the response as a promise and trying to map it into Problem[]
using "res.json", which is then being passed into BehaviorSubject
.
import { Injectable } from '@angular/core';
import { Problem } from '../models/problem.model';
import { HttpClient, HttpResponse, HttpHeaders } from '@angular/common/http';
import { BehaviorSubject} from 'rxjs';
import { Observable } from 'rxjs';
@Injectable()
export class DataService {
private problemsSource = new BehaviorSubject<Problem[]>([]);
constructor(private http: HttpClient) { }
getProblems(): Observable<Problem[]> {
this.http.get('api/problems')
.toPromise()
.then((res: Response) => {
this.problemsSource.next(res.json());
})
.catch(this.handleError);
return this.problemsSource.asObservable();
}
On a side note, as someone new to Angular
, I attempted to reuse some code from a previous project. Due to deprecation of certain APIs in Angular
, the current code implementation is no longer functioning, and I am struggling to find a workaround.