Currently, I am attempting to execute a GET request to the GitHub API using Angular2. The returned data is in JSON format, and my goal is to store it in a JSON object for further processing.
While referring to the Angular2 documentation for guidance, I encountered the following code snippet:
getHeroes (): Observable<Hero[]> {
return this.http.get(this.heroesUrl)
.map(this.extractData)
.catch(this.handleError);
However, I faced a challenge around defining the 'Hero[]' variable for the API, especially since it doesn't align with the specific structure outlined in the documentation (which includes 'id' and 'name' attributes). Therefore, I am seeking assistance in creating an Observable function that can retrieve and store the data in an object format.
In a previous Angular 1.5 project, I implemented a similar functionality as shown below:
var urlBase = 'https://api.github.com/search/repositories?q=';
var SearchOp = {};
SearchOp.getRepositories = function (search) {
SearchOp = $http.get(urlBase+ search +'&sort=star&order=desc&per_page=5')
return SearchOp;
};
In my current Angular2 implementation, I have structured my code like this:
search (term: string) {
let gitUrl = 'https://api.github.com/search/repositories';
let params = new URLSearchParams();
params.set('q', term);
let result = this.http
.get(gitUrl, { search: params })
.map(request => request.json());
console.log(result);
return result;
}
However, the output displayed in the console is:
'Observable {_isScalar: false, source: Observable, operator: MapOperator}'
Which isn't the expected result I was hoping for.