不执行的功能 [英] Function that does not execute

查看:168
本文介绍了不执行的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个插入我的文章的函数,我在我的页面中调用了这个函数,没有错误,但是没有执行下一个函数retrieveAllArticles()。

I have a function to insert my articles and I call this function in my page, there is no error, but the next function retrievedAllArticles() is not executed.

    public saveAllArticles(article) {
    for(let data in article) {
      this.db.executeSql("INSERT INTO `all_articles` (id, titre, introduction, image, redacteur_nom, redacteur_twitter, date_publication, contenu_part1, tweet, image2, contenu_part2, tweet2, image3, contenu_part3, tweet3, image4, contenu_part4, tweet4, image5, contenu_part5, tweet5, image6, contenu_part6, tweet6, image7, contenu_part7, tweet7, image8, contenu_part8, tweet8, image9, contenu_part9, tweet9, image10, contenu_part10, tweet10) VALUES (" +
        article[data].article_id + ',"' +
        article[data].article_titre + "\")", {})
        .then(() => {
          console.log("Inséré");
        }).catch(e =>
        console.log("Erreur :" + JSON.stringify(e))
      );
    }
  }


  public retrieveAllArticles() {

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

      console.log(data);

      if(data == 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;
  }

console.log(检索);没有显示,但是console.log('Inséré');显示。

The console.log("Retrieve"); is not displayed, but the console.log('Inséré'); is displayed.

我页面的构造函数:

    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.retrieveAllArticles();
    }
  }

建议后:

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.retrieveAllArticles();
        console.log("articles");
        console.log(this.allArticles);
      });

    }
  }

    public saveAllArticles(article) {

    let insertions: Array<Promise<any>> = [];
    console.log("insertions", insertions);
    for (let data in article) {
      insertions.push(this.db.executeSql("INSERT INTO `all_articles` (id, titre, introduction, image, redacteur_nom, redacteur_twitter, date_publication, contenu_part1, tweet, image2, contenu_part2, tweet2, image3, contenu_part3, tweet3, image4, contenu_part4, tweet4, image5, contenu_part5, tweet5, image6, contenu_part6, tweet6, image7, contenu_part7, tweet7, image8, contenu_part8, tweet8, image9, contenu_part9, tweet9, image10, contenu_part10, tweet10) 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))
      });
    }
  }

你能帮我吗?

提前谢谢

推荐答案

确保检索所有保存操作完成后的数据,你可以试试这个:

To make sure that you retrieve the data after all the saving operations have completed, you can try this:

public saveAllArticles(article) {
  // Make an array of all promises
  let insertions: Array<Promise<any>> = [];
  for (let data of article) {
    insertions.push(this.db.executeSql("INSERT INTO ...", {}));
  }
  // Execute all promises and retrieve all articles afterwards
  Promise.all(insertions).then(() => {
      console.log("All records have been inserted");
      this.allArticles = this.sqliteService.retrieveAllArticles();
    }).catch(e => {
      console.log("Error: " + JSON.stringify(e))
    });
}

分组承诺的方法取自 //stackoverflow.com/a/37841820/1009922>这个答案 GünterZöchbauer。

The method for grouping of promises is taken from this answer by Günter Zöchbauer.

这篇关于不执行的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆