When working in Angular, I am attempting to extract data from an endpoint. Below is the service code:
export class VideosService {
result;
constructor(private http: Http, public router: Router) { }
getVideos() {
this.http.get('http://localhost:3000/videos?sessionId=' + localStorage.getItem('sessionId'))
.map(response => response.json())
.subscribe(result => this.result = result);
localStorage.setItem('videos', this.result);
console.log(localStorage);
}
Everything seems to work fine except on the first try. On the initial attempt, I receive "undefined" as the result but subsequent attempts return the correct object.
Any advice on what mistake I might be making? Thanks!