【发布时间】:2018-11-13 05:30:43
【问题描述】:
我是一个离子程序,它使用 sqlite 来存储数据。当我将数据返回到视图时,它什么也不返回。
我设置了一个服务来获取数据。好像
read(){
this.sqlite.create({
name: DB_NAME,
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql('CREATE TABLE IF NOT EXISTS todos(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(32), datetime VARCHAR(32))', {})
.then(() => console.log("Created Table OR NOT"))
.catch(e => this.presentAlert(e));
db.executeSql('SELECT * FROM todos ORDER BY id DESC', {})
.then((response) => {
if(response.rows.length > 0){
for(var i=0; i < response.rows.length; i++){
this.my_todos.push({
id:response.rows.item(i).id,
name:response.rows.item(i).name,
datetime:response.rows.item(i).datetime
})
}
}
//this.presentAlert(JSON.stringify(this.my_todos));
return this.my_todos;
})
.catch(e => this.presentAlert(JSON.stringify(e)));
})
.catch(e => this.presentAlert(JSON.stringify(e)));
}
在 home.ts 文件中,我尝试访问类似
的服务this.todos = this.todoService.read()
当我在控制台上登录 this.todos 时。它不返回任何东西。谁能帮帮我?...
【问题讨论】:
-
哇,谢谢你回复我。我看到了帖子。我试图实施你所说的,但我似乎无法做到正确。此外,返回函数是“void”类型而不是承诺。
-
你不回数据,加
return sqlite.create().then(()=> { return db.executeSql(...); }}); -
如果这不起作用,我可以给你一个解决方法,但它很讨厌
标签: typescript ionic-framework ionic3 ionic-native