I have a function that inserts my articles and I call this function on my page. There are no errors, but the next function retrieveAllArticles() is not being 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("Inserted");
}).catch(e =>
console.log("Error :" + 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;
}
The console.log("Retrieve"); is not displayed, but the console.log('Inserted'); is displayed.
The constructor of my page :
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();
}
}
After making some changes :
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);
});
}
}
https://i.sstatic.net/vBBcr.jpg
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("Error :" + JSON.stringify(e))
});
}
}
Can someone please assist me with this issue?
Thank you in advance