2

I understand that an Observable is a dynamic object, that changes over time. I am however, looking to create a function, that will return just a single snapshot of that observable.

I have the following function, that uses AngularFire2 and returns an Observable.

import { Observable } from 'rxjs/Observable';

    findMessages(chatItem: any): Observable<any[]> {
        return this.af.database.list('/message/', {
            query: {
                orderByChild: 'negativtimestamp'
            }
        }).map(items => {
            const filtered = items.filter(
                item => ((item.memberId1 === chatItem.memberId1 && item.memberId2 === chatItem.memberId2)
                    || (item.memberId1 === chatItem.memberId2 && item.memberId2 === chatItem.memberId1))
            );
            return filtered;
        });
    }

This works perfectly, and if I display this list, it dynamically updates as items are added or removed.

Question

How do I call this function, and convert the Observable to a static array of items?

I have looked here and tried to retrofit their example to my situation, but cannot seem to do so.

Any help appreciated.

Community
  • 1
  • 1
Richard
  • 8,193
  • 28
  • 107
  • 228

1 Answers1

1

You need to subscribe to your Observable when you call your function. Actually you can't return anything from Promise or Observable because they are asynchronous. So you need to get the chain and use your variable there.

findMessages(char).subscribe((data) => {
 // it is the success function. Data is the one returned from the map
 // yourVariable = data;
});
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112