Search code examples
angularjsnode.jsangular2-services

AppComponent.html:1 ERROR TypeError: Cannot read property 'subcribe' of undefined


Hi I am new to AngularJs 2.

I am creating simple application to read Github API.

When i am starting the application with the following files, i am getting the following error:

AppComponent.html:1 ERROR TypeError: Cannot read property 'subcribe' of undefined

Below are my service and component ts files.

github.service.ts

import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';

import 'rxjs/add/operator/map';

@Injectable()

export class GithubService{
    private username: string;
    private client_id = "client_id";
    private client_secret = "client_secret";

    constructor( private _http: Http){
        console.log("Github service is ready...");
        this.username = "graphicsmanoj";
    }

    getUser(){
        this._http.get('http://api.github.com/users/'+this.username+'?client_id='+this.client_id+'&client_secret='+this.client_secret).map(res => res.json());
    }
}

profile.component.ts

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

import { GithubService } from '../services/github.service';

@Component({
    moduleId: module.id,
    selector: 'profile',
    templateUrl: 'profile.component.html',
})
export class ProfileComponent  {
    constructor(private _githubService: GithubService){
        this._githubService.getUser().subcribe(user => {
            console.log(user);
        })
    }
}

Thank you very much in advance for the solution given.


Solution

  • It shoulb be subscribe,

    Change to

    this._githubService.getUser().subscribe(user => {
                console.log(user);
    })
    

    and change your service as,

      getUser(): Observable<any> {
           return this._http.get('http://api.github.com/users/'+this.username+'?client_id='+this.client_id+'&client_secret='+this.client_secret).map(res => res.json());
        }