Ionic 3 + sqlite - modal onDidDismiss not returning data


#1

Hi, i need some help!
On my modal i insert data on a child table, the insert is success.
But when i close my modal on event onDidDismiss of the modal i calculate the data that i insert in the modal but the data seems not insert yet but on the app with the modal closed the data was successful inserted.

If i close and open the modal the data that was insert first time i open the modal is there and work. But in the same time that i close the modal and try to select the inserted data on the event onDidDismiss the data is not there yet.

Some idea ?
What can i do ?


#2

Please someone ? I really need to solve this


#3

Please post the code that doesn’t work.
Otherwise it’s impossible to help you.


#4

Modal:

//---begin modal----

  modalItensVenda(id: number, vendido: string = null) {
    const modalOptions: ModalOptions = {
      showBackdrop: false,
      enableBackdropDismiss: false,
      cssClass: "modalCSS"
      // enterAnimation: 'modal-alert-pop-in',
      // leaveAnimation: 'modal-alert-pop-out'
    };

    const modalItensVenda: Modal = this.modalCtrl.create(ModalItensVendaPage, { produto: id, vendido: vendido }, modalOptions);

    modalItensVenda.onWillDismiss((data) => {
      console.log('aqui vai fechar')
    });

    modalItensVenda.onDidDismiss((data) => {
      console.log('aqui o modal já fechou')
      this.getAllItensVenda();
      this.calculaVenda();
    });

    modalItensVenda.present();

  }
//----- end of modal -------

The problem is: I have a page and i call this modal, the modal has a insert in the sqlite database, the insert work ok but on the event “onDidDismiss” of this modal I call two methods and this methods check the insert I did before to calculate some fields, but the select does’t return any data, but if I open and close the modal again the data i inserted the first time i opened the modal is there and the select return ok.
But I need this data on this event “onDidDismiss”.
I try to call the methods in the “ionDidEnter” event of the screen i call the modal but does’t work too.

I will try to explain with this screenshots:


#5

The other two screenshots:


#7

Anyone can help me with this ?


#8

Anyone can help me with this ?


#9

can you post the code for this page?

Ive been working with Ionic v4 now but, if remember correctly, it should implement ViewController to call the ViewController.dismiss(data) on the code that executes on your ‘OK’ button. Then the onDidDismiss() should catch that data

advanced ModalController usage


#10

Does’t work! I already use the viewController please help me.

  constructor(private navCtrl: NavController,
              private toast: ToastController,
              private productProvider: ProductProvider,
              private itensVendaProvider: ItensVendaProvider,
              private navParams: NavParams,
              private view: ViewController,
              private alertCtrl: AlertController,
              private photoViewer: PhotoViewer) {

      this.parIdVenda = this.navParams.data.venda;
      this.parIdProduto = this.navParams.data.produto;
      this.parIdTabelaPreco = this.navParams.data.tabelaPreco;
      this.parVendido = this.navParams.data.vendido;

      this.itensVenda = new ItensVenda();
      this.produto = new Produto();

      if (this.parIdProduto) {
        this.id_produto = this.parIdProduto;
        this.productProvider.getDetalhe(this.id_produto)
          .then((result: any) => {
            this.product = result;
          })
      };

      if (this.parIdProduto) {
        this.productProvider.get(this.id_produto)
          .then((result: any) => {
            this.produto = result;
          })
      };

      if (this.parIdVenda) {
        this.itensVenda.id_venda = this.parIdVenda;
      };

      if (this.parIdTabelaPreco) {      
        this.itensVenda.id_tabela_preco = this.parIdTabelaPreco;
      };

      this.itensVendaProvider.getItem(this.itensVenda.id_venda, this.id_produto, this.parIdTabelaPreco)
        .then((result: any[]) => {
            this.itensVendaValida = result;
            if(this.itensVendaValida.length > 0){
              this.parVendido = 'S';
            }else{
              this.parVendido = 'N';
            }
        });
}

  closeModal(){
    this.view.dismiss();

  }
....
}

#11

Does’t work! I already use the viewController please help me.


#12

You have to pass some data in this.view.dismiss() to get it back in onDidDismiss(data)


#13

I try and have the same problem … the method “this.calculaVenda();” doesn’t recognize that i inserted the item on the modal … but if i open the modal and close again the sum() in this method works ok


#14

I don’t understand your answer.
calculaVenda() is only executed when you dismiss the modal, so it seems to do that.
Where does the problem lie then?


#15

Yes the method executed correctly, but like i said in the modal i insert that with this method:

  confirmarItem(){
    if (this.itensVenda.qt_item == 0){
      this.itensVendaProvider.remove(this.itensVenda.id_venda, this.itensVenda.id_produto, this.itensVenda.id_tabela_preco);
      this.closeModal();
    }
    else if (this.itensVenda.qt_item > 0 && this.itensVenda.qt_item % this.produto.qt_multiplo > 0){
      this.toast.create({ message: 'Quantidade do Item inválida, não é múltiplo de: '+this.produto.qt_multiplo.toString()+'.', duration: 2000, position: 'botton' }).present();
    }else{
      if (this.parVendido == 'S'){
        this.itensVendaProvider.update(this.itensVenda);
      }else{
        this.itensVenda.vendido = 'S';
        this.itensVendaProvider.insert(this.itensVenda);
      }
      this.closeModal();
    }
  }

This method in the modal call my provider itensVendaProvider to insert data:

export class ItensVendaProvider {

  constructor(private dbProvider: DatabaseProvider) { }

  public insert(itemVenda: ItensVenda) {
    return this.dbProvider.getDB()
      .then((db: SQLiteObject) => {
        let sql = " INSERT INTO itens_venda ( id_venda "
                                        +" , key_venda "
                                        +" , nr_pedido "
                                        +" , id_tabela_preco "
                                        +" , id_produto "
                                        +" , nr_item "
                                        +" , qt_item "
                                        +" , vl_desconto "
                                        +" , pc_desconto "
                                        +" , vl_unit_item "
                                        +" , vl_total_item "
                                        +" , vendido "
                                        +" ) "
                +" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
        let data = [itemVenda.id_venda
                    ,itemVenda.key_venda
                    ,itemVenda.nr_pedido
                    ,itemVenda.id_tabela_preco
                    ,itemVenda.id_produto
                    ,itemVenda.nr_item
                    ,itemVenda.qt_item
                    ,itemVenda.vl_desconto
                    ,itemVenda.pc_desconto
                    ,itemVenda.vl_unit_item
                    ,itemVenda.vl_total_item
                    ,itemVenda.vendido];
        return db.executeSql(sql, data)
          .catch((e) => console.error(e));
      })
      .catch((e) => console.error(e));
  }

But this is a child table and when i close the modal i use this method calculaVenda() to calculate the father table with the data of the child table… but in this time when i close the modal the data seems not to have inserted yet on the sqlite database but if i open the modal an close just close don’t insert anything the method calculaVenda() return data of the database…


#16

That’s because executing SQL is an asynchronous task.
This means that your app is still executing the SQL when closing the modal.

Look into promises and all will be clear.


#17

MattE i will try today, afternoon i reply if work or not. Thank u so much for now…