Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49259

Reading multiple records from storage before continuing with code

$
0
0

@Tornado55 wrote:

Hi. New to Ionic and having some problems reading from Storage…

I have saved in the Storage and array of 4 arrays of ids, like:
_abiertas = { "urgentes": ["1", "3"], "altas":["5"], "normales":[], "bajas":["6","7","9"]}

I also have one registry in Storage for each id, like:

{id: "1", numeracion: "2017-565", establecimiento: "Rio", maquina: {…}, direccion: "Calle Rio Lozoya, Nº 9", …}

Entering a page i have this 2 functions:

ionViewDidEnter() {
       this.loadIncidenciasFromStorage();
       console.log(this.incidencias.length)
       this.incidencias.forEach(incidencia => {
       ...
       }
 }

loadIncidenciasFromStorage() {
    this.incidencias = [];

    this.storage.ready().then(() => {
      this.storage.get('_abiertas').then((data) => {
        if (data != null) {
          let _abiertas = JSON.parse(data);
          _abiertas.urgentes.forEach(id => {
            this.storage.get('incidencia-'+id).then((incidencia) => {
              if (incidencia != null) {
                this.incidencias.push(JSON.parse(incidencia));
              }
            });
          });

          _abiertas.altas.forEach(id => {
            this.storage.get('incidencia-'+id).then((incidencia) => {
              if (incidencia != null) {
                this.incidencias.push(JSON.parse(incidencia));
              }
            });
          });

          _abiertas.normales.forEach(id => {
            this.storage.get('incidencia-'+id).then((incidencia) => {
              if (incidencia != null) {
                this.incidencias.push(JSON.parse(incidencia));
              }
            });
          });

          _abiertas.bajas.forEach(id => {
            this.storage.get('incidencia-'+id).then((incidencia) => {
              if (incidencia != null) {
                this.incidencias.push(JSON.parse(incidencia));
              }
            });
          });
        }
      });
    });
  }

console.log inside ionViewDidEnter keeps saying incidencias.length is undefined. I know has something to do with Promises and asyncronous loading from Storage… but im quite newbie with this.

i just want all the loads and stuff inside loadIncidenciasFromStorage done before continuing with the code…

help will be highly appreciated. Thanks

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 49259

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>