-1

I'm calling this function within an event service.

searchSessions = (searchTerm: string): Observable<ISession[]> => {
    var term = searchTerm.toLocaleLowerCase();
    var results: ISession[] = [];

    EVENTS.forEach(event => {
        var matchingSessions = event.sessions.filter(session => session.name.toLocaleLowerCase().indexOf(term) > -1);
        results = results.concat(matchingSessions);
    })

    var emitter = new EventEmitter(true);
    setTimeout(() => {
        emitter.emit(results);
    }, 100);

    return emitter;
}

Using this function in my component

searchSessions(searchTerm: string) {
    this._eventService.searchSessions(searchTerm).subscribe(sessions => {
        this.foundSessions = sessions;
    });

    console.log(this.foundSessions);
}

However every time it's called it seems to be returning the results from the last attempt, so I'm always a search behind. I've tried stepping through it but I just can't work out what the problem is. Any help would be greatly appreciated.

Cheers,

Z

Zhorian
  • 802
  • 1
  • 9
  • 15
  • Please give a comment detailing why this question received a down vote. I appreciate it was an obvious mistake but there isn't any need to cowardly down vote anonymously. – Zhorian May 10 '17 at 21:41
  • Possible duplicate of [How do I return the response from an Observable/http/async call in angular2?](http://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular2) – AT82 May 11 '17 at 09:10

1 Answers1

0

So I put the console log in the wrong place...

searchSessions(searchTerm: string) {
    this._eventService.searchSessions(searchTerm).subscribe(sessions => {
        this.foundSessions = sessions;
        //HERE
        console.log(this.foundSessions);
    });

    //console.log(this.foundSessions); NOT HERE
}
Zhorian
  • 802
  • 1
  • 9
  • 15