Search code examples
ionic-frameworkionic4lifecycleionic-native

Obtain data from sqlite Ionic 4


I have a problem with my code that when I go to look for the data that I have stored in SQLite these are executed at the end and I need them since these I need them to consume a service that will finally load the data on the screen. I leave my code below:

 import { Component, OnInit } from '@angular/core';
 import { LoadingController, Platform } from '@ionic/angular';
 import { AndroidPermissions } from '@ionic-native/android-permissions/ngx';
 import { Uid } from '@ionic-native/uid/ngx';
 import { EsqueletoServiceProvider } from '../../providers/no4get- 
 service/no4get-services';
 import { DatabaseServiceProvider } from '../../providers/database- 
 service/database-service';
 import { AlertController } from '@ionic/angular';

 @Component({
   selector: 'app-general',
   templateUrl: './general.page.html',
   styleUrls: ['./general.page.scss'],
 })
 export class GeneralPage {
   listaCategorias: any;
   token: any;
   imei: string;
   cards: any;
   loading: any;
   datosTelefono: any;


   constructor(private loadingController: LoadingController,
     public esqueletoService: EsqueletoServiceProvider,
     public databaseService: DatabaseServiceProvider,
     public uid: Uid, 
     public androidPermissions: AndroidPermissions,
     public alertCtrl: AlertController,
     public platform: Platform
     ) 
   { 
     this.listaCategorias = "1";
     this.databaseService.getSesion(1).then(result => {
       this.token = result.valor;
     })
     .catch( error => {
       this.MostrarAlerta("Noticias","","Hubo un problema al obtener los 
       datos del usuario.");
     });

     this.databaseService.getSesion(2).then(result => {
       this.imei = result.valor;
     })
     .catch( error => {
       this.MostrarAlerta("Noticias","","Hubo un problema al obtener los 
       datos del usuario.");
     });
   }



 ionViewDidEnter()
   {
     this.platform.ready().then(() => { 
       let datos = { 
         idNoticias: 0, 
         token: this.token,
         imei: this.imei
       };

       this.LoaderNoticias();
       this.esqueletoService.CargarNoticiasGeneral(datos).then((result) => 
       {
         if (result != null) 
         {
           let codigoError = result.toString().split('|');

           if(codigoError[0] == "777")
           {
             this.loadingController.dismiss();
             this.cards = result;
           }
           else if (codigoError[0] == "305")
           {
             this.loadingController.dismiss();
             this.MostrarAlerta("Noticias","",codigoError[2]);
           }
           else if (codigoError[0] == "310")
           {
             this.loadingController.dismiss();
             this.MostrarAlerta("Noticias","",codigoError[2]);
           }
           else if(codigoError[0] == "315")
           {
             this.loadingController.dismiss();
             this.MostrarAlerta("Noticias","",codigoError[2]);
           }
           else if(codigoError[0] == "000")
           {
             this.loadingController.dismiss();
             this.MostrarAlerta("Noticias","","Estimado usuario, hubo un 
             error inesperado.");
           }   
         } else {
           this.loadingController.dismiss();
           this.MostrarAlerta("Noticias","","Estimado usuario, hubo un 
           error inesperado.");
         }
       }, (err) => {
         this.loadingController.dismiss();
         this.MostrarAlerta("Noticias","","Estimado usuario, hubo un error 
         inesperado.");
       });
     });
   }

Currently, when the ionViewDidEnter () event is executed, it creates a variable called data that is where I want to send an array to my service, but the token and IMEI fields up to that point are undefined. By following the path to the code once the entire ionViewDidEnter () event is executed, the code just allocates the data of the token and IMEI variables that are in the constructor. The question is, how can I get this data to be obtained before the ionViewDidEnter () event is executed?

My code is being executed in visual studio code and Ionic 4. I thank you in advance for all the possible help you can give me, while I will continue investigating.


Solution

  • If your data relies on other async data then you just have to keep nesting it until you slowly go insane :P

    Remove the code from your constructor and then put it all in the ionViewDidEnter() with nesting:

    ionViewDidEnter() {
        this.platform.ready().then(() => {
            this.listaCategorias = "1";
            this.databaseService.getSesion(1).then(result => {
                this.token = result.valor;
    
                this.databaseService.getSesion(2).then(result => {
                    this.imei = result.valor;
    
                    let datos = {
                        idNoticias: 0,
                        token: this.token,
                        imei: this.imei
                    };
    
                    this.LoaderNoticias();
                    this.esqueletoService.CargarNoticiasGeneral(datos).then((result) => {
                        if (result != null) {
                            let codigoError = result.toString().split('|');
    
                            if (codigoError[0] == "777") {
                                this.loadingController.dismiss();
                                this.cards = result;
                            }
                            else if (codigoError[0] == "305") {
                                this.loadingController.dismiss();
                                this.MostrarAlerta("Noticias", "", codigoError[2]);
                            }
                            else if (codigoError[0] == "310") {
                                this.loadingController.dismiss();
                                this.MostrarAlerta("Noticias", "", codigoError[2]);
                            }
                            else if (codigoError[0] == "315") {
                                this.loadingController.dismiss();
                                this.MostrarAlerta("Noticias", "", codigoError[2]);
                            }
                            else if (codigoError[0] == "000") {
                                this.loadingController.dismiss();
                                this.MostrarAlerta("Noticias", "", "Estimado usuario, hubo un error inesperado.");
                            }
                        } else {
                            this.loadingController.dismiss();
                            this.MostrarAlerta("Noticias", "", "Estimado usuario, hubo un error inesperado.");
                        }
                    }, (err) => {
                        this.loadingController.dismiss();
                        this.MostrarAlerta("Noticias", "", "Estimado usuario, hubo un error inesperado.");
                    });
                }).catch(error => {
                    this.MostrarAlerta("Noticias", "", "Hubo un problema al obtener los datos del usuario.");
                });
            }).catch(error => {
                this.MostrarAlerta("Noticias", "", "Hubo un problema al obtener los datos del usuario.");
            });
        });
    }