sqlite에 삽입 한 후 내 기사를 검색하고 싶지만 검색 할 변수는 null입니다.sqlite에서 데이터 복구가 작동하지 않습니다.
그것은이 게시물의 계속이다 : Function that does not execute
는 당신이 데이터를 한 후, 데이터베이스와 테이블을 만들 삽입하고 검색 할 수있는 sqliteservice.ts 있습니다. 데이터가 삽입되었지만 변수에서 검색 할 수 없습니다. 내 페이지의 생성자에서 함수를 호출합니다. 데이터를 삽입하고 함수를 호출
내 서비스를 검색 할 수 있습니다 :
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))
});
}
}
난을 console.log (삽입)을 수행 할 때; 루프 전에는 내 콘솔에 결과가 없습니다. 루프에 다음 오류가 있습니다.
Uncaught (in promise) TypeError: Cannot read property 'push' of undefined
그리고 this.allArticles가 null입니다.
기사를 검색하는 기능으로,이 함수는 호출되지 않습니다. console.log ('Retrieve'); 표시되지 않은 :
이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;
});
}
내 페이지의 생성자 : saveAllArticles 문제
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;
}
}
아무도 도와 줄 수 없습니까? : –
당신은 어디에서 retrieveAllArticles를 호출하고 있습니까? 그리고 무엇입니까? this.allArticles = this.sqliteService.allArticles;? –
saveAllArticles()에서 retrieveAllArticles()를 호출하고 있습니다. 아니오 this.allArticles = this.retrieveAllArticles(); –