不执行的功能

问题描述 投票:0回答:1

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

    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é');被展示。

我的页面的构造函数:

    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);
      });

    }
  }

enter image description here

    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))
      });
    }
  }

你能帮我吗 ?

先感谢您

angular sqlite typescript ionic-framework
1个回答
0
投票

要确保在完成所有保存操作后检索数据,可以尝试以下操作:

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))
    });
}

承诺分组的方法来自this answerGünter Zöchbauer

© www.soinside.com 2019 - 2024. All rights reserved.