2

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');
  }
}
0

2 Answers 2

9

Since you are mapping like so:

.map((res) => { res.json() })

you need to use return

.map((res) => { return res.json() })

or use just:

.map(res => res.json())

DEMO

Sign up to request clarification or add additional context in comments.

Comments

0

Your [(ngModel)] will not work in this case so you need to use ElementRef to refer the input text box

<input type="text" name="searchStr" #input (keyup.enter)="searchMusic(input.value)" class="form-control" placeholder="Search music here..."/>

Modify your method as

searchMusic(searchStr:string) {
    return this._spotifyservice.searchMusic(searchStr)
      .subscribe(data => {
        console.log(data);
        this.searchResult = data;
       },
        error => {
          console.log(error)
        })
      }   

In your component declare an element ref as

@ViewChild('input') input: ElementRef;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.