【问题标题】:Item scope in TypeScript(JavaScript)TypeScript(JavaScript) 中的项目范围
【发布时间】:2016-05-06 07:37:00
【问题描述】:

我想在ionic2中使用sqlite数据库。

我可以在以下代码中连接到数据库并成功检索项目数据。 但我不能推入 this.items 数组。

错误提示:

undefined 不是对象(正在评估 'this.items')

有谁知道问题出在哪里? 我猜这是可变范围,但我不确定。

import {Page, Platform} from 'ionic-angular';
declare var sqlitePlugin:any;
declare var plugins:any;

@Page({
  templateUrl: 'build/pages/getting-started/getting-started.html'
})
export class GettingStartedPage {
  items: Array<{title: string}>;
  constructor(platform: Platform) {
    platform.ready().then(()=>{
      this.getData();
    });
  }

  getData(){
    sqlitePlugin.openDatabase({name: 'encrypted.db', key: 'Password', location: 'default'}, function(db) {
      db.transaction(function(tx) {
          var query: string = "SELECT * FROM items";
          this.items = []; <-- error happens at this row.
          tx.executeSql(query, [], function(tx, resultSet) {
            //alert("name: " + resultSet.rows.item(0).name);
            this.items.push({
              title: resultSet.rows.item(0).name
            });            
          }, function(error) {
            alert('SELECT error: ' + error.message);
            console.log('SELECT error: ' + error.message);
          });
        }, function(error) {
          alert('transaction error: ' + error.message);
          console.log('transaction error: ' + error.message);
        }, function() {
          console.log('transaction ok');
        });
      }, function(error){
        alert('error' + error.message);
    });
  }  
}

【问题讨论】:

    标签: javascript typescript angular ionic2


    【解决方案1】:

    使用() =&gt; 而不是function ()

    arrow functions 会一直指向类而不是当前函数。

    import {Page, Platform} from 'ionic-angular';
    declare var sqlitePlugin:any;
    declare var plugins:any;
    
    @Page({
      templateUrl: 'build/pages/getting-started/getting-started.html'
    })
    export class GettingStartedPage {
      items: Array<{title: string}>;
      constructor(platform: Platform) {
        platform.ready().then(()=>{
          this.getData();
        });
      }
    
      getData(){
        sqlitePlugin.openDatabase({name: 'encrypted.db', key: 'Password', location: 'default'}, (db) => {
          db.transaction((tx) => {
              var query: string = "SELECT * FROM items";
              this.items = []; <-- error happens at this row.
              tx.executeSql(query, [], (tx, resultSet) => {
                //alert("name: " + resultSet.rows.item(0).name);
                this.items.push({
                  title: resultSet.rows.item(0).name
                });            
              }, (error) => {
                alert('SELECT error: ' + error.message);
                console.log('SELECT error: ' + error.message);
              });
            }, (error) => {
              alert('transaction error: ' + error.message);
              console.log('transaction error: ' + error.message);
            }, () => {
              console.log('transaction ok');
            });
          }, (error) =>{
            alert('error' + error.message);
        });
      }  
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-29
      • 2018-12-22
      • 1970-01-01
      • 2011-04-04
      • 2017-01-16
      • 2020-04-26
      • 2017-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多