我是离子1开发的新手。我试图显示我插入到我的SQLite表中的数据。我希望在Chrome控制台中看到它们。但是,我收到以下错误:
ionic.bundle.js:26799 TypeError: Cannot read property 'transaction' of null
at Object.execute (ng-cordova.min.js:9)
at ChildScope.$scope.insert (controllers.js:22)
at fn (eval at compile (ionic.bundle.js:27643), <anonymous>:4:209)
at ionic.bundle.js:65429
at ChildScope.$eval (ionic.bundle.js:30400)
at ChildScope.$apply (ionic.bundle.js:30500)
at HTMLButtonElement.<anonymous> (ionic.bundle.js:65428)
at defaultHandlerWrapper (ionic.bundle.js:16792)
at HTMLButtonElement.eventHandler (ionic.bundle.js:16780)
at triggerMouseEvent (ionic.bundle.js:2953)
这是我的控制器代码的片段。我有一个单独的控制器js文件。
.controller('salesCtrl', ['$scope', '$stateParams','$cordovaSQLite', '$ionicPlatform',
function ($scope, $stateParams,$cordovaSQLite, $ionicPlatform) {
$ionicPlatform.ready(function () {
var db = null;
if (window.cordova) {
db = $cordovaSQLite.openDB({ name: "pos.db" }); //device
console.log("not in browser");
} else{
db = window.openDatabase("pos.db", '1', 'my', 1024 * 1024 * 100); // browser
console.log("browser");
}
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS items(id integer primary key AUTOINCREMENT,firstname text NOT NULL,lastname text NOT NULL)");
});
$scope.insert = function(){
var query="INSERT into items(firstname,lastname) VALUES(?,?)";
$cordovaSQLite.execute(db,query,["name1","name2"]).then(function(result){
console.log(result.rows);
},function(error){
console.log(error);
});
}
}])
这些是我安装的cordova插件:
cordova-plugin-console 1.0.6 "Console"
cordova-plugin-device 1.1.5 "Device"
cordova-plugin-splashscreen 4.0.2 "Splashscreen"
cordova-plugin-statusbar 2.2.2 "StatusBar"
cordova-plugin-whitelist 1.3.2 "Whitelist"
cordova-sqlite-storage 1.5.3 "Cordova sqlite storage plugin"
ionic-plugin-keyboard 2.2.1 "Keyboard"