0

I'm trying to implement a function that check if a price is in a range.

The data are located in the IndexedDB and I'm using Dexie to work with it.

In this moment I can't compile my soluzion

public checkPrizeChange(codCli: string, codList: string, codArt: string, price: number): Observable<any> {

    this._WebDBService.Listino_Articoli.filter(function (i){
      return (i.codLis == codList && i.codArt == codArt);
    }).toArray().then(
      data => {
        if(data.length != 1)
        {
          return Observable.of(false)
        }
        else{
          if(data[0].prezzoMin >= price && data[0].prezzoMax <= price)
            return Observable.of(true)
          else
            return Observable.of(false)
        }
      }
    );
   }

I don't know what is the properly way to work with dexie...

I just have to check some fields in a row of a DexieTable, nothing hard and return an Observable...

Thanks to support

4

2 回答 2

1

Dexie 方法 toArray() 返回一个 Promise。你想返回一个 Observable。

可能,您应该执行以下操作:

public checkPrizeChange(codCli: string, codList: string, codArt: string, price: number): Observable<boolean> {

  return Observable.fromPromise(this._WebDBService.Listino_Articoli.filter(i => i.codLis == codList && i.codArt == codArt).toArray().then(
    data => {
      if(data.length != 1)
      {
        return false;
      }
      else{
        if(data[0].prezzoMin >= price && data[0].prezzoMax <= price)
          return true;
        else
          return false;
      }
    }
  );
}

此外,您似乎可以利用复合索引“[codLis+codArt]”来加快查询速度。如果使用 Dexie 2.0,可以重写您的查询以利用索引:

this._WebDBService.Listino_Articoli.where({
  codLis: codLis,
  codArt: codArt
}).toArray()
于 2017-10-06T10:48:10.353 回答
1

return在此行之前添加:

return this._WebDBService.Listino_Articoli.filter(function (i){
于 2017-10-06T09:33:19.400 回答