-1

I am trying to setup a service on Angular version 7 but I'm having an issue on res.json() which throws the error Property 'json' does not exist on type 'Object'. This is my service's code:

import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from "rxjs";
import 'rxjs/add/operator/map';
import { map } from 'rxjs/operators';

import {
    SVCParameters,
    SVCResult
} from "./types";

const SERVER_URL: string = 'api/';

@Injectable()
export class IrisService {

    constructor(private http: HttpClient) {
    }

    public trainModel(svcParameters: SVCParameters): Observable<SVCResult> {
        return this.http.post(`${SERVER_URL}train`, svcParameters).pipe(map(res => res.json()));

    }
}

The following is my types.ts code:

export class SVCParameters {
C: number = 2.0;
}

export class SVCResult{
accuracy: number;
}
georgeawg
  • 48,608
  • 13
  • 72
  • 95

1 Answers1

0

Angular 7 HttpClientModule, by default returns response as JSON, there's no need to map the response as res.json() just remove your pipe operator. Everything will work fine.

import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from "rxjs";
import 'rxjs/add/operator/map';
import { map } from 'rxjs/operators';

import {
    SVCParameters,
    SVCResult
} from "./types";

const SERVER_URL: string = 'api/';

@Injectable()
export class IrisService {

    constructor(private http: HttpClient) {
    }

    public trainModel(svcParameters: SVCParameters): Observable<SVCResult> {
        return this.http.post(`${SERVER_URL}train`, svcParameters);

    }
}
Nithya Rajan
  • 4,722
  • 19
  • 30
  • I get the following error instead: Type 'Observable' is not assignable to type 'Observable'.   The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?     Property 'accuracy' is missing in type 'Object' but required in type 'SVCResult'. – Christopher Kinyua Mar 06 '19 at 09:27
  • try `any`, if everything works fine, then try to figure out which type is suited and whether the response has all the properties of the type or not. – Nithya Rajan Mar 06 '19 at 09:30
  • 1
    I've replaced 'Observable' with 'any' and the error is gone. I really hope this is what I'm supposed to do because I'm just learning Angular. – Christopher Kinyua Mar 06 '19 at 09:41
  • @ChristopherKinyua its not – Jota.Toledo Mar 06 '19 at 11:02