【问题标题】:How to return return data with Ionic sqlite如何使用 Ionic sqlite 返回返回数据
【发布时间】: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(()=&gt; { return db.executeSql(...); }});
  • 如果这不起作用,我可以给你一个解决方法,但它很讨厌

标签: typescript ionic-framework ionic3 ionic-native


【解决方案1】:

//嗨, //你不能这样做 this.todos = this.todoService.read(). //你应该编写promise来从sqlite获取数据并在你的//组件中使用它们。 //我希望读取函数在服务文件(todoService.ts)中。像 //this 一样更改它。

//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

【讨论】:

  • 这对我有用,非常感谢。不过我还有一个小问题。在视图中,每当我刷新或更改页面时,项目都会重复...请问您知道为什么吗?
  • 请您详细说明您的问题
  • 我的主页是我显示待办事项列表的地方。它看起来像*ngFor="let todo of itemList" 列表显示一切正常,但是当我转到另一个页面时,假设我转到该过程完成后添加新 Todo 的页面并将页面设置回根目录。我复制的待办事项列表。如果我退出应用程序并再次打开它,一切都会恢复正常。我在某处读到过它,但我不确定细节,那个人说这与订阅和取消订阅有关。这有什么意义吗?感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
相关资源
最近更新 更多