我是一个使用sqlite存储数据的离子程序。当我将数据返回到视图时,它什么都不返回。
我设置了一个服务来获取数据。它看起来像
this.todos = this.todoService.read()
在home.ts文件中,我尝试访问
这样的服务this.todos
当我在控制台上记录{{1}}时。它没有返回任何东西。有人可以帮帮我吗?...
答案 0 :(得分:3)
//您好, //你不能这样做.todos = this.todoService.read()。 //您应该编写promises来从sqlite获取数据并在//组件中使用它们。 //我希望read函数在服务文件(todoService.ts)中。改变它就像//这个。
//Hi,
//You cannot do this this.todos = this.todoService.read().
//You should write promises to get data from sqlite and use them in your //component.
//I expect read function is in the service file(todoService.ts). Change it like //this.

public read() {
return new Promise((resolve,reject) => {
var db = new SQLite();
var query = "CREATE TABLE IF NOT EXISTS todos(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(32), datetime VARCHAR(32))";
db.create({
name: "data.db",
location: "default"
}).then((db1: SQLiteObject) => {
db1.executeSql(query,[])
.then((res) => {
let todos = [];
if(res.rows.length > 0 ) {
todos.push(res.rows.item(0))
}
resolve(todos)
},(error) => {
reject(error);
});
})
}
// The promise returns an array of objects
In the page component
// declare this
public itemList : Array<Object>;
// In the constructor
this.itemList = [];
this.todoService.read().then((result) => {
this.itemList = <Array<Object>> result;
// you will get the retrieved data from sqlite from this array
console.log(this.itemList)
})
)
// This should work Please update me in case of any issues
&#13;