0

I want to use sqlite database in ionic2.

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.

Error says:

undefined is not an object(evaluating '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);
    });
  }  
}

1 Answer 1

3

Use () => instead of 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);
    });
  }  
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.