【问题标题】:Promise<any> chaining in Ionic2 SQLite native pluginIonic2 SQLite 本机插件中的 Promise<any> 链接
【发布时间】:2017-02-01 02:54:40
【问题描述】:

我是 Javascript 承诺的新手,我无法进行正确的链接。

我正在使用native SQLite Ionic2 plugincordova sqlite plugin 包装在类似promise 的API 中。我想等待 SQL 查询解析,然后使用结果集来获取数据,因为我正在执行 SELECT 子句。 SQLite 示例以及我的代码如下:

  [...]
  executeQuery(query: string, params: any){
    let db = new SQLite();
    return db.openDatabase({
      name: 'applicationData.db',
      location: 'default'
    }).then(() => {
      db.executeSql(query, params).then((resultSet) => {
        return resultSet;
      }, (err) => {
        console.error('Unable to execute sql: ', err);
      });
    }, (err) =>{
      console.error('Unable to open database: ', err);
    });
  }
    
  getConfig(){
    let query = 'SELECT * from configuration';
    this.executeQuery(query, []).then((resultSet) => {
      resultSet.rows.item(0);
    }, (err) => {
    
    });
  }
  [...]

我得到了:

类型“void”上不存在属性“rows”。

[19:13:19] 转译失败

[19:13:19] ionic-app-script 任务:“构建”

[19:13:19] 错误:错误

L23:  this.executeQuery(query, []).then((resultSet) => {
L24:    resultSet.rows.item(0);
L25:  }, (err) => {

【问题讨论】:

    标签: angular typescript promise ionic2 cordova-plugins


    【解决方案1】:

    您需要在executeQuery 方法中返回db.executeSql 调用,否则您的Promise 只会返回void,就像转译器说的那样:

    executeQuery(query: string, params: any){
        let db = new SQLite();
        return db.openDatabase({
            name: 'applicationData.db',
            location: 'default'
        }).then(() => {
            //here
            return db.executeSql(query, params).then((resultSet) => {
                return resultSet;
            }, (err) => {
                console.error('Unable to execute sql: ', err);
            });
        }, (err) =>{
            console.error('Unable to open database: ', err);
       });
    }
    

    【讨论】:

    • 非常感谢,这正是问题所在。我很难理解 Promise 的逻辑。
    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2016-12-24
    • 2017-03-07
    • 1970-01-01
    • 2017-08-11
    • 2017-02-07
    相关资源
    最近更新 更多