ionic 3 sqlite not return value

无人久伴 提交于 2019-12-24 23:13:52

问题


I create provider to insert , select , delete , update data in sqlite

this my code in database.service.ts to get data

getData(){

 return  this.sqlite.create({
        name: 'data.db',
        location: 'default'
      }) 
      .then((db: SQLiteObject) => {
         db.executeSql('select * from  downloaded order by ID', {})
         .then(res => {
            for(let i = 0;i < res.row.length; i++){
                this.data.push({
                    id: res.row.item(i).id,
                    name: res.row.item(i).name,
                    url: res.row.item(i).URL
                })
            }
            return this.data;
           }).catch(e => {
            console.log(e);
            });


       })
}

and this my code in list.ts to receive the data

getData(){
this.data =  this.DB.getData();

}

but no values return


回答1:


You must publish an event with your data and listen your event in where you wait for data.

Because:

this.sqlite.create({ name: 'data.db', location: 'default' }) .then()

Is async call and dosnt return value. You should do like this:

this.events.publish('getData', dataRows);

And listen to event in where you want:

events.subscribe('getData', (data) => {
 //Do somethings
 });


来源:https://stackoverflow.com/questions/50667665/ionic-3-sqlite-not-return-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!