Search code examples
angulartypescriptes6-promisedexie

Dexie - ToArray() **Type 'Promise[]>' is not assignable to type '[]'.**


The Requirement is to return two simple arrays from the localdb.

The function is:

public getCaricamentoVeloceConf(): Observable<any> {
    let res = new RespOrdiniGetsceltecaricamentoveloce();

    res.tipo = this._WebDBService.Configurazione_CV_Scelte.toArray();
    res.ordinamento = this._WebDBService.Configurazione_CV_Ordinamento.toArray();

    return Observable.of(res);
  }

The error message I get is:

Type 'Promise' is not assignable to type 'Tipo[]'

I think that is because the ToArray() function returns a promise.

Actually what I need is, to compose the res object with the two arrays but I don't know how to combine the two promise toArray() methods

Any solution to this?


Solution

  • Try this:

    import 'rxjs/add/observable/fromPromise'; 
    import { Observable } from "rxjs/Observable";
    
    public getCaricamentoVeloceConf(): Observable<any> {   
        var res = new RespOrdiniGetsceltecaricamentoveloce();
        return Observable.fromPromise(
            this._WebDBService.Configurazione_CV_Scelte.toArray().then(tipo => {     
                res.tipo = tipo;
                return this._WebDBService.Configurazione_CV_Ordinamento.toArray();
            }).then(ordinamento => {
                res.ordinamento = ordinamento;
                return res;
            })
         );
      }