Person
Person

Reputation: 2269

Type 'void' is not assignable to type 'any[]' when sending data to service

I'm getting error on this line this.apiService.getGifs(searchValue.value).subscribe(data=>this.results=data);

My code works but I'm getting this error so I want to figure out how to fix it. I believe it is becaouse of this part .subscribe(data=>this.results=data); but without it code doesn't work.

apiService

@Injectable()
export class ApiService {

private results = new BehaviorSubject([]);

public getResults$(){
   return this.results.asObservable();
}

  constructor (private http: Http) {}
  link = 'http://api.giphy.com/v1/gifs/search?q=';
  ApiKey = 'MaklOv0qiTVslRvJlBShgpr46LWPs5Z0';

  public getGifs(searchValue:string) {
     return this.http.get(this.link + searchValue +'&api_key='+ this.ApiKey)
     .map((res:Response) => this.results.next(res.json().data));
  }
}

Component which send data to service

export class SearchComponent implements OnInit {
  results = []

  constructor(private apiService: ApiService) { }

  ngOnInit() {
  }

  onSearch(searchValue) {
   this.apiService.getGifs(searchValue.value).subscribe(data=>this.results=data);
  }
}

Component which receives data

export class OutputComponent implements OnInit {
  results = [];
    constructor(private apiService: ApiService){
    apiService.getResults$().subscribe(data => this.results = data)
}

Upvotes: 0

Views: 249

Answers (1)

El houcine bougarfaoui
El houcine bougarfaoui

Reputation: 37373

in addition to what @jonrsharpe said this should work :

 import 'rxjs/add/operator/do';

 public getGifs(searchValue:string) {
     return this.http.get(this.link + searchValue +'&api_key='+ this.ApiKey)
     .map((res:Response) => res.json().data)
     .do( data => this.results.next(data))
  }

Upvotes: 1

Related Questions