Following a tutorial where the instructor is building a spotify music search with the spotify api. Followed every single step the instructor did but the problem is my data never comes back, I get undefined and nothing shows up on screen. However if i hit the api directly in my browser the json data is displayed but never displays in my app.
Actually I've seen a lot of questions like this but none of them solved my problem. I thought it might be a network related issue but when i set a breakpoint I could see the result in the response but nothing ever gets loaded into view, undefined is being loaded into the console
**search.component.html**
<h1>Need Music?</h1>
<p class="lead">
Use the ngSpotify app to browse new releases of your favorite songs. See what your favorite artistes are up to.
</p>
<form>
<div class="form-group">
<input type="text" name="searchStr" [(ngModel)]="searchStr" (keyup.enter)="searchMusic()" class="form-control" placeholder="Search music here..."/>
</div>
</form>
**search.component.ts**
import { Component, OnInit } from '@angular/core';
import { SpotifyService } from '../../Services/spotify.service';
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {
constructor(private _spotifyservice: SpotifyService) { }
searchStr: string;
searchResult;
searchMusic() {
return this._spotifyservice.searchMusic(this.searchStr)
.subscribe(data => {
console.log(data);
this.searchResult = data;
},
error => {
console.log(error)
})
}
ngOnInit() {
}
}
**spotify.service.ts**
import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { Observable } from 'rxjs/Rx'
@Injectable()
export class SpotifyService {
private searchUrl: string;
constructor(private _http: Http) { }
searchMusic(str: string, type = 'artist') {
this.searchUrl = `http://api.spotify.com/v1/search?query=${str}&offset=0&limit=20&type=${type}&market=US`
return this._http.get(this.searchUrl)
.map((res) => { res.json() })
.catch(this.handleError);
}
handleError(error: Response) {
return Observable.throw(error || 'server error');
}
}