1

I have problem while using and retrieving data from sqlite with Ionic and Angular. Actually I know where the problem appears but not how to solve that in this case. I'm pretty knew to this and didn't found a solution for that yet (I search since 2 days...).

in my DB Provider I want to fill a List via sql query. The problem is it is async.

Here you can see the code. The problem happens right after eintrag in getPlants.

  //Get all Plants
    getPlants() {
      return this.database.executeSql("SELECT * FROM plants", []).then((data) => {
        if (data.rows.length > 0) {
          for (var i = 0; i < data.rows.length; i++) {
            this.plants.push({
            plantId: data.rows.item(i).plantId,
            fk_categoriesId: data.rows.item(i).fk_categoriesId,
            name: data.rows.item(i).name,
            favourite : data.rows.item(i).favourite,
            foto: data.rows.item(i).foto,
            aussaatzeitpunkt: data.rows.item(i).aussaatzeitpunkt,
            medium: data.rows.item(i).medium,
            umgebung: data.rows.item(i).umgebung,
            anbauflaeche: data.rows.item(i).anbauflaeche,

         eintrag: this.getOneEntry(data.rows.item(i).plantId).then((d:Tagebucheintraege[])=>{
              console.log("eintrag einer pflanze"+d[0].notiz);
              return d;
            })
           });
          }
        }
        return this.plants;
      }, err => {
        console.log('Error: ', err);
        return [];
      });
    }

    //Get Entry to Plant ID
    getOneEntry(id:number){
        return this.database.executeSql("SELECT * FROM entries WHERE fk_plantId="+id, []).then((data) => {
        if (data.rows.length > 0) {
          for (var i = 0; i < data.rows.length; i++) {
            this.entries.push({
            eintragId: data.rows.item(i).eintragId,
            fk_plantId: data.rows.item(i).fk_plantId,
            datum: data.rows.item(i).datum,
            notiz: data.rows.item(i).notiz,
            groesePflanze: data.rows.item(i).groesePflanze,
            phWert: data.rows.item(i).phWert,
            luftfeuchttigkeit: data.rows.item(i).luftfeuchttigkeit,
            erdfeuchtigkeit: data.rows.item(i).erdfeuchtigkeit,
            ecWert: data.rows.item(i).ecWert,
            temperatur: data.rows.item(i).temperatur,
            schaedlinge: data.rows.item(i).schaedlinge,
            mangelerscheinung: data.rows.item(i).eintragId,
            fotopfad: data.rows.item(i).fotopfad,
            beleuchtunngstaerke: data.rows.item(i).beleuchtunngstaerke,
            beleuchtungsabstand: data.rows.item(i).beleuchtungsabstand,
            groeseToepfe: data.rows.item(i).groeseToepfe,
            mengeWasserProTag: data.rows.item(i).MengeWasserProTag,
            artDuengerUndWerteproTag: data.rows.item(i).artDuengerUndWerteproTag,
            mengeduenger: data.rows.item(i).mengeduenger,
            tagVegi: data.rows.item(i).tagVegi,
            tagFlo: data.rows.item(i).tagFlo,
            tagFru: data.rows.item(i).tagFru,
           });
          }
        }
        return this.entries;
      }, err => {
        console.log('Error: ', err);
        return [];
      })
    }

The log console.log("eintrag einer pflanze"+d[0].notiz); works just fine but I Think eintrag is not yet filled when the plants are returned. Maybe it will work with Oberservables but I don't know how to use it yet and espacially in this case. Thank you for your answers! Try to access the data, The Error

3
  • Yes, you are returning plants before anything has been filled to it, you should add it inside the callback (then) after your for-loop, same for the other request. Commented Sep 30, 2017 at 14:05
  • and as for perhaps doing this a bit "nicer", you'd maybe want something like this: stackoverflow.com/a/42829674/7741865 as you mentioned about perhaps using observables in your question :) Commented Sep 30, 2017 at 14:14
  • I will check your Link. But plants ist filled ;) Just eintrag is not... Commented Sep 30, 2017 at 15:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.