Проблема: Я не могу получить какие-либо данные от своего сервиса in-memory-api.Функция http get возвращает только «undefined».
Краткое описание приложения: У меня есть функция в data-store.service.ts, getAlert (id) ,которая вызывает функцию backend-data.service, backendDataService.getAlert (id) и возвращает this.http.get (url).
Когда backendDataService.getAlert (id)Вызывается , он выдает ошибку через .pipe (catchError ...) с ошибкой undefined (консоль читает: "Ошибка сервера getAlert: undefined").
ЗдесьbackendDataService вызывает эти ошибки:
getAlert(alertId: number): Observable<any> {
const url = '/alert/' + alertId;
return this.http
.get(url)
.pipe(catchError(this.handleError('getAlert', [])));
}
Когда это вызывается, вызывается catchError () и регистрирует "undefined" как ошибку.
package.json
"@angular/core": "^6.1.7",
....
"angular-in-memory-web-api": "^0.6.1",
....
app.module.ts
@NgModule...
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,
RouterModule,
RouterModule.forRoot([]),
HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService, {
dataEncapsulation: false
}),
...
providers: [
{
provide: DateAdapter,
useClass: MomentDateAdapter,
deps: [MAT_DATE_LOCALE]
}
],
bootstrap: [AppComponent],
entryComponents: [
ConfirmationDialogComponent,
DismissDialogComponent,
DeviceNotesDialogComponent,
AccountNotesDialogComponent,
DatetimeRangePickerDialogComponent
]
...
data-store service.ts
import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
import { BackendDataService } from './backend-data.service';
@Injectable({
providedIn: 'root'
})
export class DataStoreService {
private alertId: any;
public alert$: any = new BehaviorSubject([]);
constructor(
private route: ActivatedRoute,
public backendDataService: BackendDataService
) {
this.getAlertId();
}
getAlertId() {
this.route.queryParams.subscribe(params => {
this.alertId = params['aid'];
if (this.alertId) {
this.getAlert(this.alertId);
}
});
}
<<<<< THIS IS CALLS THE BACKEND FUNC THAT IS ERR >>>>>
getAlert(id: number) {
this.backendDataService.getAlert(id).subscribe(
res => {
const alertData: any = res;
this.alert$.next(alertData);
},
err => console.log(`Error: ${err}`)
);
}
backend-data-service.ts
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { catchError, map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class BackendDataService {
private base_url: string = '/api/v1/';
private alert_endpoint: string = 'alert/';
constructor(private http: HttpClient) {}
getAlert(alertId: number): Observable<any> {
const url = '/alert/' + alertId;
return this.http
.get(url)
.pipe(catchError(this.handleError('getAlert', [])));
}
}
in-memory-data.service.ts
import { InMemoryDbService } from 'angular-in-memory-web-api';
export class InMemoryDataService implements InMemoryDbService {
createDb() {
const alert = [
{
id: 1492011
...more data...
}
];
return { alert };
}
}