My services
search (packageName: string, refresh = false): Observable<PackageInfo>
{
const options = createHttpOptions(packageName, refresh);
this.searchResults = this.http.get(searchUrl + packageName, options) as Observable<PackageInfo>;
return this.searchResults
}
I can do
this.searchResults.subscribe(repoUrl => console.log(repoUrl.repos_url))
This will display the url that is in my Observable. I need to save the repos_url, so I can make a second http.get call. I am not sure how to do this. My thoughts where to save the subscribe, but that returns undefined in the console.log.Which means nothing is returning from what I read it is because value only exist as long as the Observable is active. I am stuck right now. I got my Profile search component working and my Repo list component, but I need the Profile component to pass the repo url to the Repo List component.
profile-search.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, BehaviorSubject } from 'rxjs';
import { HttpErrorHandler, HandleError } from '../error-handler.service';
export interface PackageInfo {
login: string
name: string
avatar_url: string
repos_url: string
}
export const searchUrl = 'https://api.github.com/users/';
const httpOptions = {
headers: new HttpHeaders({
'x-refresh': 'true'
})
};
function createHttpOptions(packageName: string, refresh = false) {
const headerMap = refresh ? {'x-refresh': 'true'} : {};
const headers = new HttpHeaders(headerMap) ;
return { headers };
}
@Injectable({
providedIn: 'root'
})
export class PackageSearchService {
searchResults: Observable<PackageInfo>;
private handleError: HandleError;
constructor(
private http: HttpClient,
httpErrorHandler: HttpErrorHandler
) {
this.handleError = httpErrorHandler.createHandleError('Service');
}
search (packageName: string, refresh = false): Observable<PackageInfo>
{
const options = createHttpOptions(packageName, refresh);
this.searchResults = this.http.get(searchUrl + packageName, options) as Observable<PackageInfo>;
return this.searchResults
}
}
My first thought was to create a blank variable with string type.
export class PackageSearchService {
searchResults: Observable<PackageInfo>;
private handleError: HandleError;
repoUrl: string; <== new
constructor(
private http: HttpClient,
httpErrorHandler: HttpErrorHandler
) {
this.handleError = httpErrorHandler.createHandleError('Service');
}
search (packageName: string, refresh = false): Observable<PackageInfo>
{
const options = createHttpOptions(packageName, refresh);
this.searchResults = this.http.get(searchUrl + packageName, options) as Observable<PackageInfo>;
this.repoUrl = this.searchResults.subscribe(userRepo => userRepo.repo_url)
return this.searchResults
}
repo-list.component.ts
import { Component, OnInit } from '@angular/core';
import { RepoListComponent } from './repo-list.services';
import { PackageSearchService } from '../profile-search/profile-search.service';
import { RepoList } from '../repoList';
import { Observable, Subscription } from 'rxjs';
@Component({
selector: 'repo-list',
templateUrl: './repo-list.html',
styleUrls: ['../profile-search/profile-search.component.css']
})
export class UsersRepoComponent implements OnInit {
repo: RepoList[] = [];
constructor( private repoService : RepoListComponent,
private searchService: PackageSearchService){}
ngOnInit(){
this.getRepoList();
}
getRepoList(): void {
this.repoService.getRepoReturn()
.subscribe(repo => this.repo = repo);
}
getRepoUrl(){
this.searchService.repoUrl;
}
}
repoUrl: string this.repoUrl = this.searchResults.subscribe(repoUrl => console.log(repoUrl.repos_url))
– Dustin