I've been learning Angular (6) the last couple days, and I've been trying to make a request to the omdb api. Here's the code, retrieve a random movie id and looking for that specific one. But I can't reach the api or the function, I have a couple components, the buttons and the container for the movies.
import { Component, OnInit } from '@angular/core';
import { Http, Response } from '@angular/http';
import { map } from 'rxjs/operators';
@Component({
selector: 'app-options',
templateUrl: './options.component.html',
styleUrls: ['./options.component.css']
})
export class OptionsComponent implements OnInit {
omdbData = ['0386676', '1865718', '0098904', '4508902', '0460649', '2861424', '0108778', '1305826', '0096697', '0149460'];
randomItem = this.omdbData[Math.floor(Math.random() * this.omdbData.length - 1) + 1];
result: Object;
constructor(private http: Http) {
// console.log(this.randomItem);
// this.searchData();
}
id = this.randomItem;
searchData() {
this.http.get('https://cors-anywhere.herokuapp.com/http://www.omdbapi.com/?i=tt' + this.id + '&apikey=')
.pipe(map(
(res: Response) => {
const result = res.json();
console.log(result);
}
));
}
ngOnInit() {
this.searchData();
}
}