Return data happens before changing it, async/sync problem

My problem here is when I call the first block of code my return is undefined, cause return diarios is called before I define my “diarios” array, notice that my return of SelectTable function is ok, it returns my data. I dindt]'t find a solution, plox any help, haha :smiley:

If any other info is needed please tell me.

This is where I’m calling my data get

DiarioDeBordo(diario?: DiarioBordo): Observable<Array<DiarioBordo>>{
      let diarios = new Array<DiarioBordo>();
      if(diario == null || diario == undefined){
        this.db.selectTable('diario_bordo').then(res => {
          diarios = res;

          diarios.map((diario) =>{
            return this.db.selectTable('diario_bordo_file', 'WHERE id_increment = ' + diario.id_increment).then(arquivos => {
              diario.arquivos = [];
              diario.arquivos = arquivos;
            });
          })
          
          // for(let i = 0; i < diarios.length; i++){
          //   this.db.selectTable('diario_bordo_file', 'WHERE id_increment = ' + diarios[i].id_increment).then(arquivos => {
          //     diarios[i].arquivos = [];
          //     diarios[i].arquivos = arquivos;
          //   });
          // }
          this.db.exportDB();
          return diarios;
        });
      }else{
        return null;
      }
      
  }

And I call it like :

this.clienteService.DiarioDeBordo().subscribe(diarios => {
      this.diarios = diarios;
    });

And finally my data get is

selectTable(table_name: string, where?: string): any{
    let obj = [];
    if(where == null){
      return this.db.executeSql("SELECT * FROM " + table_name, []).then(data => {
        if(data.rows.length > 0 ){
          if(data.rows.length == 1){
            return data.rows.item(0);
          }
          for(let i=0; i < data.rows.length; i++){
            obj.push(data.rows.item(i));
          }
        }else{
          obj.push("NULL");
        }
        return obj;
      },err => {
      console.log('Error: ', err);
      return [];
      });
    }else{
      return this.db.executeSql("SELECT * FROM " + table_name + " " + where, []).then(data => {
        if(data.rows.length > 0 ){
          if(data.rows.length == 1){
            return data.rows.item(0);
          }
          for(let i=0; i < data.rows.length; i++){
            obj.push(data.rows.item(i));
          }
        }else{
          obj.push("NULL");
        }
        return obj;
      }, err => {
        console.log('Erro: ', err);
        return [];
      });
    }
  }

**Return Error: ** ERROR Error: Uncaught (in promise): TypeError: Cannot read property ‘subscribe’ of undefined
TypeError: Cannot read property ‘subscribe’ of undefined

Something like this should do what you want:

DiarioDeBordo(diario?: DiarioBordo): Observable<Array<DiarioBordo>>{
  let diarios = new Array<DiarioBordo>();
  if(!!diario) {
    return Observable.of(null);
  }

  return Observable.fromPromise(this.db.selectTable('diario_bordo').then(res => {
    diarios = res;

    return Promise.all(diarios.map((diario) =>{
      return this.db.selectTable('diario_bordo_file', 'WHERE id_increment = ' + diario.id_increment).then(arquivos => {
        diario.arquivos = [];
        diario.arquivos = arquivos;
      });
    }));
  }).then(_ => {
    this.db.exportDB();
    return diarios;
  }));
}
1 Like

worked, Really thank you!!