0

I would like retrieve my articles after my insert in sqlite, but my variable that is supposed to retrieve them is null.

It's the continuation of this post : Function that does not execute

I have a sqliteservice.ts which allows you to create the database and tables, then insert and retrieve the data. The data is inserted but I can not retrieve it in a variable. I call my functions in the constructor of my page.

My service that inserts the data and calls the function to retrieve them :

      public saveAllArticles(article) {

    let insertions: Array<Promise<any>> = [];
    for (let data in article) {
      insertions.push(this.db.executeSql("INSERT INTO `all_articles` (id, titre) VALUES (" +
        article[data].article_id + ',"' +
        article[data].article_titre + "\")", {}))
      Promise.all(insertions).then(() => {
        console.log("All records have been inserted");
        this.allArticles = this.retrieveAllArticles();
      }).catch(e => {
        console.log("Erreur :" + JSON.stringify(e))
      });
    }
  }

When I do a console.log(insertions); before the loop, there is not result in my console, in the loop there is this error :

Uncaught (in promise) TypeError: Cannot read property 'push' of undefined

And this.allArticles is null.

The function to retrieve my articles, this function is not called because the console.log('Retrieve'); is not displayed :

    public retrieveAllArticles() {

    console.log("Retrieve");
    this.allArticles = [];
    this.db.executeSql('SELECT id FROM `all_articles`', {})
      .then((data) => {

      if(data == null) {
        console.log('null');
        return;
      }

      if(data.rows) {
        if(data.rows.length > 0) {
          for(let i = 0; i < data.rows.length; i++) {
            this.allArticles.push(data.rows.item(i).article_id);
          }
        }
      }

        return this.allArticles;
    });
  }

The constructor of my page :

  allArticles: any;
  observable$;

  constructor(public navCtrl: NavController,
              public modalCtrl: ModalController,
              protected articlesService: ArticlesService,
              protected sqliteService: SqliteService,
              private network: Network,
              public toastCtrl: ToastController,
              public platform: Platform)
  {
    this.observable$ = this.articlesService.getAllArticles();

    if (this.platform.is('cordova')) {
      sqliteService.createDatabaseFile();

      this.articlesService.getAllArticles().subscribe(article => {
        this.allArticles = article;
        this.sqliteService.saveAllArticles(this.allArticles);
      });

      this.allArticles = this.sqliteService.allArticles;
    }
  }
Valentin Harrang
  • 1,081
  • 2
  • 17
  • 34

1 Answers1

0

Something wrong with saveAllArticles, it should probably look like this:

public saveAllArticles(article) {
  return Promise.all(//return something so you know when saveAllArticles is done
    //assuming article is an array if it's itterable you may try Array.from(article)
    article.map(
      data=>
        this.db.executeSql(
          "INSERT INTO `all_articles` (id, titre) VALUES (" +
            article[data].article_id + ',"' +
            article[data].article_titre + "\")"
          ,{}
        )
    )
  )
  .then(() => {
    console.log("All records have been inserted");
    this.allArticles = this.retrieveAllArticles();
  }).catch(e => {
    console.log("Erreur :" + JSON.stringify(e))
  });
}

retrieveAllArticles returns undefined, not sure if that is what you wanted. You may want that function to return something.

HMR
  • 37,593
  • 24
  • 91
  • 160
  • Yes, I would simply like to retrieve my articles in a variable to exploit it in the constructor of my page – Valentin Harrang Dec 19 '17 at 10:29
  • No idea ? it's been 4 days that I block on this problem :/ – Valentin Harrang Dec 19 '17 at 10:45
  • @ElGecko_76 I think you have to do some investigation on how JavaScript event queue works. Then you'd know why a function you write would return a promise and what that promise is. I tried to explain this [here](https://stackoverflow.com/a/47678417/1641941). – HMR Dec 19 '17 at 13:59
  • I especially would like help to find a solution to my problem, it does not matter if I do not use a promise, as long as it works :/ – Valentin Harrang Dec 19 '17 at 16:44