问题
I am a new learner of NativeScript. I am following a video tutorial on Coursera to create an Android project using NativeScript, and I got the above error that is made by this part of my code:
process-httpmsg.service.ts
:
import { Injectable } from '@angular/core';
import { throwError } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ProcessHTTPMsgService {
constructor() { }
public handleError(error: HttpErrorResponse | any) {
let errMsg: string;
if (error.error instanceof ErrorEvent) {
errMsg = error.error.message;
} else {
errMsg = `${error.status} - ${error.statusText || ''} ${error.message}`;
}
return throwError(errMsg);
}
}
dish.services.ts
:
import { Injectable } from '@angular/core';
import { Dish } from '../shared/dish';
import { Observable } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { baseURL } from '../shared/baseurl';
import { ProcessHTTPMsgService } from './process-httpmsg.service';
@Injectable({
providedIn: 'root'
})
export class DishService {
constructor(private http: HttpClient,
private processHTTPMsgService: ProcessHTTPMsgService) { }
getDishes(): Observable<Dish[]> {
return this.http.get<Dish[]>(baseURL + 'dishes')
.pipe(catchError(this.processHTTPMsgService.handleError));
}
getDish(id: string): Observable<Dish> {
return this.http.get<Dish>(baseURL + 'dishes/' + id)
.pipe(catchError(this.processHTTPMsgService.handleError));
}
getFeaturedDish(): Observable<Dish> {
return this.http.get<Dish[]>(baseURL + 'dishes?featured=true').pipe(map(dishes => dishes[0]))
.pipe(catchError(this.processHTTPMsgService.handleError));
}
}
menu.component.html
:
<ActionBar title="Menu" class="action-bar">
</ActionBar>
<StackLayout class="page">
<ListView [items]="dishes" class="list-group" *ngIf="dishes">
<ng-template let-dish="item">
<StackLayout orientation="horizontal" class="list-group-item">
<Image row="0" col="0" rowSpan="2" height="108" width="108" [src]="BaseURL + dish.image" class="thumb p-16"></Image>
<GridLayout class="list-group-item" rows="auto *" columns="*">
<Label row="0" col="0" [text]="dish.name" class="list-group-item-heading"></Label>
<Label row="1" col="0" class="list-group-item-text" [text]="dish.description"></Label>
</GridLayout>
</StackLayout>
</ng-template>
</ListView>
<ActivityIndicator busy="true" *ngIf="!(dishes || errMess)" width="50" height="50" class="activity-indicator"></ActivityIndicator>
<Label *ngIf="errMess" [text]="'Error: ' + errMess"></Label>
</StackLayout>
menu.component.ts
:
import { Component, OnInit, Inject } from '@angular/core';
import { Dish } from '../shared/dish';
import { DishService } from '../services/dish.service';
@Component({
selector: 'app-menu',
moduleId: module.id,
templateUrl: './menu.component.html',
styleUrls: ['./menu.component.css']
})
export class MenuComponent implements OnInit {
dishes: Dish[];
errMess: string;
constructor(private dishService: DishService,
@Inject('baseURL') private baseURL) { }
ngOnInit() {
this.dishService.getDishes()
.subscribe(dishes => this.dishes = dishes,
errmess => this.errMess = <any>errmess);
}
}
I don't know which other parts of the project's code is needed to put, so let me know that please.
EDIT:
baseURL.ts
:
// export const baseURL = "http://localhost:3000/";
export const baseURL = "http://192.168.1.5:3000/";
回答1:
As I already mentioned you can not do error.error instanceof ErrorEvent
as ErrorEvent
is an interface not class. You could do error.error instanceof HttpErrorResponse
.
You can not hit http://localhost:3000
from your Simulator / Emulator, you are suppose to hit your api by your machine's IP as they are in same network but technically different (simulated) devices.
来源:https://stackoverflow.com/questions/56577315/error-referenceerror-errorevent-is-not-defined-for-nativescript-app