1

I'm creating an Ionic 2 project that shows github users and their details. I've created Service file called github-users.ts and compiler showing me this error in .json() method.

Property 'json' doesn't exist on type 'Object'

Here my github-users.ts source file

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

import { User } from "../../models/user";

@Injectable()
export class GithubUsersProvider {
  githubApiUrl = 'api.github.com';

  constructor(public http: HttpClient) { 
    console.log("Hello Github Users service");
   }

  load(): Observable<User[]> {
    return this.http.get(`${this.githubApiUrl}/users`)
      .map(res => <User[]>res.json());
  }
}

And I'm getting this error from console

core.js:1449 ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[GithubUsersProvider -> HttpClient]: 
  StaticInjectorError(Platform: core)[GithubUsersProvider -> HttpClient]: 
    NullInjectorError: No provider for HttpClient!
Error: StaticInjectorError(AppModule)[GithubUsersProvider -> HttpClient]: 
  StaticInjectorError(Platform: core)[GithubUsersProvider -> HttpClient]: 
    NullInjectorError: No provider for HttpClient!
    at _NullInjector.get (core.js:1003)
    at resolveToken (core.js:1301)
    at tryResolveToken (core.js:1243)
    at StaticInjector.get (core.js:1111)
    at resolveToken (core.js:1301)
    at tryResolveToken (core.js:1243)
    at StaticInjector.get (core.js:1111)
    at resolveNgModuleDep (core.js:10896)
    at _createClass (core.js:10933)
    at _createProviderInstance$1 (core.js:10907)
    at _NullInjector.get (core.js:1003)
    at resolveToken (core.js:1301)
    at tryResolveToken (core.js:1243)
    at StaticInjector.get (core.js:1111)
    at resolveToken (core.js:1301)
    at tryResolveToken (core.js:1243)
    at StaticInjector.get (core.js:1111)
    at resolveNgModuleDep (core.js:10896)
    at _createClass (core.js:10933)
    at _createProviderInstance$1 (core.js:10907)
    at c (polyfills.js:3)
    at Object.reject (polyfills.js:3)
    at NavControllerBase._fireError (nav-controller-base.js:223)
    at NavControllerBase._failed (nav-controller-base.js:216)
    at nav-controller-base.js:263
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (core.js:4760)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at polyfills.js:3
1
  • import HttpClientModule in your app.module.ts Commented Jul 20, 2018 at 6:15

2 Answers 2

2

You need to import HttpClientModule to your app.module.ts under imports

imports:[
  HttpClientModule
]
Sign up to request clarification or add additional context in comments.

2 Comments

Now I'm getting new error. ERROR TypeError: res.json is not a function
when you are using httpclient you do not need to use res.json. just return res
0

Your error is = [No provider for HttpClient!] this type of error occurs when you does not define http module in app.modules.ts file. so firstly check it.

exp: import { HttpModule } from '@angular/http';

imports: [ HttpModule]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.