【发布时间】: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