我有一个插入我的文章的函数,我在我的页面中调用了这个函数,没有错误,但是下一个函数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);
});
}
}
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))
});
}
}
你能帮帮我吗?
提前谢谢
答案 0 :(得分: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 answer Günter Zöchbauer。