Angular2 / Nativescript:Rest服务中的TypeError,在Web应用程序中没有任何问题

时间:2017-01-11 13:39:02

标签: rest angular angular2-nativescript

我正忙于与本地服务器通信的小应用程序。当应用程序启动时,我使用Nativescript-localstorage模块检查LocalStorage中的IP地址...如果尚未设置IP,则应用程序导航到可以输入IP的设置视图。当输入IP并点击提交按钮时,IP用于Rest服务的/ ping端点的GET方法,该方法确认输入了正确的IP /服务器已启动...我继续获得以下内容错误:

error:

TypeError:  error.json is not a function

我猜这种情况发生在我的Rest服务的ping功能中,但无法弄清楚为什么......这项服务工作正常,在另一个网络应用程序中没有错误。知道是什么导致了这个吗?

更新: 因此,在输入正确的IP时,我似乎只会收到此错误。当我输入随机错误的IP时,我得到“服务器错误”作为响应......

my code:

settings.component.ts:

import { Component,} from '@angular/core';

import { RestService } from '../../services/rest.service';
import { LocalStorageService } from '../../services/local-storage.service';

@Component({
  selector: 'settings',
  templateUrl: './pages/settings/settings.component.html'
})
export class SettingsComponent {
  ipSaved: boolean = false;
  error: boolean = false;
  ip: string;

  constructor(private restService: RestService, private localStorageService: LocalStorageService) {

    }

    submitIP() {
        if (this.ip !== null && this.ip !== ''){
            console.log("ip before get: " + this.ip);
            this.restService.ping(this.ip)
            .subscribe(
                (res) => {

                    this.localStorageService.saveLocalIP(this.ip);
                    this.restService.init(this.ip);
                    this.ipSaved = true;
                },
                (res) => {
                    console.log(res);
                    this.error = true;
                }
            );      
        console.log("ip: " + this.localStorageService.findLocalIP());
        } else {
            alert("Warning: IP field can't be empty!");
        }
    }

    dismissError() {
        this.error = false;
    }


}


settings.component.html:

<StackLayout class="page">
<Label class="h1 title m-x-auto " text="Settings"></Label>
<StackLayout class="form" *ngIf="!IPSaved && !error">
<StackLayout class="input-field">
<TextField class="input input-border" placeholder="Enter IP" [(ngModel)]="ip"></TextField>
</StackLayout>
<Button class="btn btn-primary" text="Submit" (tap)="submitIP()"></Button>
</StackLayout>
<StackLayout *ngIf="IPSaved && !error">
    <Label class="h1" text="IP Saved successfully"></Label>
    <Button class="btn btn-primary" text="Done" [nsRouterLink]="['/home']"></Button>
</StackLayout>
<StackLayout *ngIf="error">
<Label class="body text-danger" text="ERROR:  The IP address entered doesn't seem to be correct.  Make sure the device is connected to the local wireless network and try again..."></Label>
<Button class="btn btn-primary" (tap)="dismissError()"></Button>
</StackLayout>

</StackLayout>


my ping function from my rest.service.ts:


ping(ip: string) : Observable<Response> {
      let tempApiUrl: string = "http://" + ip + "/BRMServices/WebEnquiry/ping";
      return this.http.get(tempApiUrl, this.headers)
      .map((res: Response) => res.json())
      .catch((error: any) => Observable.throw(error.json().error || 'Server error'));
  }

1 个答案:

答案 0 :(得分:0)

查看catch块中错误对象的类型。在您的代码中,它的类型为任何,这是无效的。相反,它也应该是响应类型如下。

ping(ip: string) : Observable<Response> {
      let tempApiUrl: string = "http://" + ip + "/BRMServices/WebEnquiry/ping";
      return this.http.get(tempApiUrl, this.headers)
      .map((res: Response) => res.json())
      // below line is modified
      .catch((error: Response) => Observable.throw(error.json().error || 'Server error'));
  }