0
votes

I am building a nativescript app and get these error when do tns build android:

src/app/services/cabin.service.ts(21,86): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'Response'. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? Property 'type' is missing in type 'Object'. src/app/services/cabin.service.ts(27,86): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'Response'. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? src/app/services/cabin.service.ts(33,86): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'Response'. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?

This is my cabin.service.ts

import { Injectable } from '@angular/core';
import { Cabin } from '../shared/cabin';
import { Observable } from 'rxjs';
import { Http, Response } from '@angular/http';
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { baseURL } from '../shared/baseurl';
import { ProcessHTTPMsgService } from './process-httpmsg.service';
import { map } from "rxjs/operators";
import { catchError } from "rxjs/operators";

@Injectable()
export class CabinService {

  cabins: Cabin[];

  constructor(private http: HttpClient,
              private processHTTPMsgService: ProcessHTTPMsgService) { }

  getCabins(): Observable<Cabin[]> {
    return this.http.get(baseURL + 'cabins')
                    .pipe(map(res => { return this.processHTTPMsgService.extractData(res); }),
                    catchError(error => { return this.processHTTPMsgService.handleError(error); }));
  }

  getCabin(id: number): Observable<Cabin> {
    return  this.http.get(baseURL + 'cabins/'+ id)
                    .pipe(map(res => { return this.processHTTPMsgService.extractData(res); }),
                    catchError(error => { return this.processHTTPMsgService.handleError(error); }));
  }

  getFeaturedCabin(): Observable<Cabin> {
    return this.http.get(baseURL + 'cabins?featured=true')
                    .pipe(map(res => { return this.processHTTPMsgService.extractData(res)[0]; }),
                    catchError(error => { return this.processHTTPMsgService.handleError(error); }));
  }

}

line 21, 23, 33 has this code:

 .pipe(map(res => { return this.processHTTPMsgService.extractData(res); }),

I am new to typescript and nativescript

This is my processHttpMessage.service import { Injectable } from '@angular/core';

import { Observable } from 'rxjs';
import { Http, Response } from '@angular/http';
import { throwError } from 'rxjs';

@Injectable()
export class ProcessHTTPMsgService {

  constructor() { }

  public extractData(res: Response) {
    let body = res.json();
    return body || { };
  }

  public handleError (error: Response | any) {
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    return throwError(errMsg);


  }
}
1

1 Answers

0
votes

While using HttpClient by default you will receive the JSON data, unless you modify the responseType or observe parameters.

res is your JSON object already, so it should be

public extractData(res: any) {
    // Unless you have further extraction to be done, this method is not required at all
    return res;
}