2017-12-19 22 views
0

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; 
    } 
    } 
+0

아무도 도와 줄 수 없습니까? : –

+0

당신은 어디에서 retrieveAllArticles를 호출하고 있습니까? 그리고 무엇입니까? this.allArticles = this.sqliteService.allArticles;? –

+0

saveAllArticles()에서 retrieveAllArticles()를 호출하고 있습니다. 아니오 this.allArticles = this.retrieveAllArticles(); –

답변

0

뭔가, 아마 다음과 같아야합니다

public saveAllArticles(article) { 
    return Promise.all(//return something so you know when saveAllArticles is done 
    //assuming article is an array if it's itterable you may try Array.from(article) 
    article.map(
     data=> 
     this.db.executeSql(
      "INSERT INTO `all_articles` (id, titre) VALUES (" + 
      article[data].article_id + ',"' + 
      article[data].article_titre + "\")" 
      ,{} 
     ) 
    ) 
) 
    .then(() => { 
    console.log("All records have been inserted"); 
    this.allArticles = this.retrieveAllArticles(); 
    }).catch(e => { 
    console.log("Erreur :" + JSON.stringify(e)) 
    }); 
} 

retrieveAllArticles하는 경우 확실하지, undefined를 반환 그것이 당신이 원하는 것입니다. 함수가 뭔가를 반환하기를 원할 수도 있습니다.

+0

예, 저는 단순히 변수에서 내 기사를 검색하여 내 기사의 생성자에서 악용하려고합니다. 페이지 –

+0

잘 모르시겠습니까? 이 문제에 대해 차단한지 4 일이 지났습니다./ –

+0

@ ElGecko_76 JavaScript 이벤트 큐가 작동하는 방식에 대해 조사해야한다고 생각합니다. 그렇다면 왜 당신이 쓰는 함수가 약속을 되찾고 그 약속이 무엇인지 알 것입니다. 나는 이것 [여기] (https://stackoverflow.com/a/47678417/1641941)을 설명하려고 노력했다. – HMR