4

Hi i have problem with my code.

import { Injectable } from '@angular/core';
import { Car } from "./models/car";
import { Observable } from "rxjs/Observable";
import { Http } from "@angular/http";
import 'rxjs'

@Injectable()
export class CarsService {
   private apiUrl = "http://localhost:3000/api/cars";
   constructor(private http : Http) { }

    getCars() : Observable<Car[]> {
        return this.http.get(this.apiUrl)
           .map((res) => res.json())
    }
}

With this code i have error:

this.http.get(...).map is not a function

but when i add:

import 'rxjs/add/operator/map'

Still have problem but error is:

Cannot read property 'map' of undefined

Can you help me? Thanks

4
  • what is the angular version Commented Dec 21, 2017 at 2:07
  • Angular CLI: 1.5.0 Node: 6.11.4 OS: win32 x64 Angular: 5.0.1 ou its 5.0.1 I thought I was using version 4 Commented Dec 21, 2017 at 2:09
  • 1
    Angular 5 no longer includes Http. It has been superceded by HttpClient. Commented Dec 21, 2017 at 2:17
  • angular-http-get-with-typescript-error-http-get-map-is-not-a-function-in-n Commented Dec 21, 2017 at 2:28

4 Answers 4

4

As mentioned by others Http is deprecated, use HttpClient instead. HttpClient parses your response to an Object, so you remove the mapping of the response. Also, to avoid type checking errors, tell Angular what kind of response you are expecting.

So import HttpClientModule and add it to imports array, after BrowserModule. In your Service import HttpClient, inject it in your constructor and use it the following way:

import { Injectable } from '@angular/core';
import { Car } from "./models/car";
import { Observable } from "rxjs/Observable";
import { HttpClient } from "@angular/common/http";
import 'rxjs'

@Injectable()
export class CarsService {
   private apiUrl = "http://localhost:3000/api/cars";
   constructor(private httpClient : HttpClient) { }

    getCars() : Observable<Car[]> {
      // tell Angular you are expecting an array of 'Car'
      return this.httpClient.get<Car[]>(this.apiUrl)
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Okey so i import my HttpClientModule and update my code. I run my server this localhost:3000/api/cars and it work. Everything is ok i see my Car[] But still in browser i have: Cannot read property 'map' of undefined Maybe i Can ignore this or not? If everything is ok?
Well you are not mapping anywhere anymore? Have you tried restarting your project? Or are you using map somewhere else?
Wait like 5 min i add in answer my another code and tell you something.
I add answer can you check?
Everything is okay now. Thanks for help @AJT_82
2

With angular5 HttpClient implementation already includes inner using of the map.so it works for you automatically.

just update it as

 getCars() : Observable<Car[]> {
     return this.http.get(this.apiUrl)
 }

Also make sure you are using HttpClient instead of Http.

You can read more about this here

8 Comments

So what i have to do?
@MarekPatyna check the link attached
did the answer help
Okey now i update my http and delete this: .map((res) => res.json())
And in my cmder i have this error : error TS2322: Type 'Observable<Object>' is not assignable to type 'Observable<Car[]>'.
|
0

Edit your code

import { Injectable } from '@angular/core';
import { Car } from "./models/car";
import { Observable } from "rxjs/Observable";
import { Http } from "@angular/http";


@Injectable()
export class CarsService {
   private apiUrl = "http://localhost:3000/api/cars";
   constructor(private http : Http) { }

   getCars() : Observable<Car[]> {
     return this.http.get(this.apiUrl)
   }
}

since it automatically parse the response as JSON. You don't have to explicitly parse it.

Comments

0
ngOnInit() {
 this.loadCars();

}

loadCars() : void {
 this.carsService.getCars().subscribe((cars) => {
  this.cars = cars;
  this.countTotalCost();
 })
}

So i move this this.countTotalCost();

from ngOnInit to loadCars

and now .map its ok.

I have only this error: Expression has changed after it was checked. Previous value: 'undefined'. Current value: 'NaN'.

<div class="row" *ngIf="grossCost"> //this line error/error context
 <div class="col-sm-12">
  <div class="total-cost">
   <p class="text-right">TOTAL GROSS COST: {{ grossCost}} PLN</p>
  </div>
 </div>
</div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.