Dstore Memory filter returns empty data unless applying .forEach

183 Views Asked by At

I have a "dstore/Memory" object declared like so:

this.flightStore = new Memory({
    data: flights,
    idProperty: 'myId'
});

I want to get back a filtered collection to display. So I write this:

var test = this.flightStore.filter({matchType:view});

That returns a 'test' object but the 'data' field is null. Yet when I write identical code but add on a forEach function:

this.flightStore.filter({matchType:view}).forEach(function(flight) { ... }

This works perfectly and I get back my filtered results one-by-one. From there I can build an array for my filtered collection. I'm curious though as to why my first line doesn't work and if there is a way to get it to work without having to loop through every single result and manually build my array.

Thanks!

1

There are 1 best solutions below

0
On

The filter method only applies a filter, it does not fetch data. The forEach method fetches data and applies the callback to it. If you want to fetch the data after applying a filter, call fetch:

var dataPromise = this.flightStore.filter({matchType: view}).fetch();

If you are using a synchronous store (dstore/Memory) the fetchSync method is available:

var testData = this.flightStore.filter({matchType: view}).fetchSync();