简体   繁体   English

TypeScript(JavaScript)中的项目范围

[英]Item scope in TypeScript(JavaScript)

I want to use sqlite database in ionic2. 我想在ionic2中使用sqlite数据库。

I could connect to the database and retrieved items data successfully in the following code. 我可以通过以下代码连接到数据库并成功检索项目数据。 But I could have not push into this.items array. 但是我可能还没有推入this.items数组。

Error says: 错误提示:

undefined is not an object(evaluating 'this.items') 未定义不是对象(评估“ this.items”)

Does anyone know what the problem is? 有人知道问题出在哪里吗? I guess it's variable scope but I'm not sure. 我想这是可变范围,但我不确定。

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);
    });
  }  
}

Use () => instead of function () 使用() =>代替function ()

With arrow functions this keeps pointing to the class instead the current function. 使用箭头功能时,它始终指向该类,而不是当前函数。

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);
    });
  }  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM