I keep encountering an issue where I am always receiving 'undefined' in my component code. Any assistance on this matter would be greatly appreciated.
When I attempt to write to the console, it consistently returns 'undefined'.
I am currently working with Angular2 and utilizing Visual Studio Code as my editor. Below you will find both my service and component code:
**Service Code**
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
export interface GitHubUser {
html_url: string;
avatar_url: string;
login: string;
score: string;
}
@Injectable({
providedIn: 'root'
})
export class GitHubServiceService {
constructor(private _http: HttpClient) {
}
getGitHubData(_searchTerm): Observable<GitHubUser[]> {
return this._http.get<GitHubUser[]>("https://api.github.com/search/users?q="+_searchTerm);
}
}
Component Code
import { Component } from '@angular/core';
import { GitHubServiceService, GitHubUser } from './git-hub-service.service';
import { filter, switchMap, debounceTime, distinctUntilChanged } from 'rxjs/operators';
import { FormControl } from '@angular/forms';
import { Observable, of } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
users: GitHubUser[];
data$: Observable<GitHubUser[]>;
title = 'app';
constructor(private _githubService: GitHubServiceService){
this.data$ = this._githubService.getGitHubData('greg');
this.data$.subscribe(users => this.users = users);
console.log(this.users);
console.log(this.users.length);
}
}