我是离子1的新手。我一直在开发一个使用SQLite的应用程序。它在浏览器上工作正常,但当我在Android设备上运行它或模拟器应用程序无法正常工作。调试后,我得到以下错误:
0 009385 error LiveReload disabled because the browser does not seem to support web sockets
1 009537 log SELECT * FROM items
2 009552 error TypeError: Cannot call method 'transaction' of null
at Object.execute (http://192.168.151.2:8100/js/ng-cordova.min.js:9:23737)
at new <anonymous> (http://192.168.151.2:8100/js/controllers/salesCtrl.js:51:32)
at Object.instantiate (http://192.168.151.2:8100/lib/ionic/js/ionic.bundle.js:18015:14)
at $controller (http://192.168.151.2:8100/lib/ionic/js/ionic.bundle.js:23417:28)
at [object Object].appendViewElement (http://192.168.151.2:8100/lib/ionic/js/ionic.bundle.js:59908:24)
at Object.render (http://192.168.151.2:8100/lib/ionic/js/ionic.bundle.js:57901:41)
at Object.init (http://192.168.151.2:8100/lib/ionic/js/ionic.bundle.js:57821:20)
at [object Object].render (http://192.168.151.2:8100/lib/ionic/js/ionic.bundle.js:59767:14)
at [object Object].register (http://192.168.151.2:8100/lib/ionic/js/ionic.bundle.js:59725:10)
at updateView (http://192.168.151.2:8100/lib/ionic/js/ionic.bundle.js:65400:23), <ion-nav-view name="side-menu21" class="view-container" nav-view-transition="android">
3 009607 error Uncaught Error: Database location or iosDatabaseLocation setting is now mandatory in openDatabase call., http://192.168.151.2:8100/plugins/cordova-sqlite-storage/www/SQLitePlugin.js, Line: 565
这是我的启动器页面代码。标题图标和标题未显示在设备上的位置:
angular.module('app.salesCtrl', ['ngCordova'])
.controller('salesCtrl', ['$scope', '$state', '$stateParams', '$cordovaSQLite', '$ionicPlatform', '$ionicPopup',
function ($scope, $state, $stateParams, $cordovaSQLite, $ionicPlatform, $ionicPopup) {
$scope.category = {};
$scope.categories = ["Food", "Beverages", "Others"];
$scope.flag = 0;
$scope.cartPage = function () {
$state.go('cart');
}
$ionicPlatform.ready(function () {
$scope.delete = function () {
var query = "delete from items";
$cordovaSQLite.execute(db, query).then(function (result) {
console.log(result.rows.length);
}, function (error) {
console.log(error);
});
}
});
$scope.items = [];
//EXPERIMENT CODE STARTS
//$scope.selectAll = function() {
//console.log($scope.category.catSelected);
if (window.cordova) {
var query = "SELECT * FROM items";
console.log(query);
$cordovaSQLite.execute(db, query, []).then(function (res) {
if (res.rows.length > 0) {
// console.log("SELECTED -> " + res.rows.item(7).itemname + " " + res.rows.item(7).cashier);
for (var i = 0; i < res.rows.length; i++) {
$scope.items.push({
itemname: res.rows.item(i).itemname,
price: res.rows.item(i).price,
quantity: res.rows.item(i).quantity,
});
//$scope.items=$scope.items;
}
} else {
console.log("No results found");
}
}, function (err) {
console.error("error=>" + err);
});
}
//EXPERIMENTS CODE ENDS
//CHANGE SELECT CODE STARTS==========================================
$scope.showSelectValue = function (mySelect) {
$scope.items.splice(0, $scope.items.length);
var query = "SELECT * FROM items WHERE category=" + "'" + mySelect + "'";
console.log(query);
$cordovaSQLite.execute(db, query, []).then(function (res) {
if (res.rows.length > 0) {
// console.log("SELECTED -> " + res.rows.item(7).itemname + " " + res.rows.item(7).cashier);
for (var i = 0; i < res.rows.length; i++) {
$scope.items.push({
itemname: res.rows.item(i).itemname,
price: res.rows.item(i).price,
});
}
} else {
console.log("No results found");
}
}, function (err) {
console.error("error=>" + err);
});
//all
}
//CHANGE SELECT CODE ENDS==============================
//GET QUANTITY STARTS
$scope.getQuantity = function (item) {
console.log(item.itemname);
$scope.data = {};
// An elaborate, custom popup
var myPopup = $ionicPopup.show({
template: '<input type="number" ng-model="data.quantity">',
title: 'Enter item quantity',
subTitle: 'Number of items',
scope: $scope,
buttons: [
{
text: 'Cancel',
onTap: function (e) {
$scope.flag = 1;
}
},
{
text: '<b>Ok</b>',
type: 'button-balanced',
onTap: function (e) {
if (!$scope.data.quantity) {
//don't allow the user to close unless he enters wifi password
e.preventDefault();
} else {
return $scope.data.quantity;
}
}
}
]
});
myPopup.then(function (res) {
console.log("RES IS " + res);
if (res != undefined) {
//EXPERIMENT CODE STARTS
var query = "UPDATE items SET quantity =" + "'" + res + "'" + " WHERE itemname=" + "'" + item.itemname + "' AND price=" + "'" + item.price + "'";
console.log(query);
$cordovaSQLite.execute(db, query, []).then(function (result) {
console.log("Updated " + res);
}, function (err) {
console.error("error=>" + err);
});
//EXPERIMENT CODE ENDS
console.log('Tapped!', res);
//IONIC ALERT STARTS
var alertPopup = $ionicPopup.alert({
title: 'Item added to the Cart',
template: 'click Ok to see Cart',
buttons: [
{
text: '<b>Ok</b>',
type: 'button-balanced',
}
]
});
alertPopup.then(function (res) {
console.log('Done');
});
} else {
console.log("Cancelled");
}
//IONIC ALERT ENDS
});
}
//GET QUANTITY ENDS
}])
答案 0 :(得分:1)
正如我所见,db
变量从未初始化。这就是为什么你有一个空例外:TypeError: Cannot call method 'transaction' of null
你应该做这样的事情来初始化数据库:
var db = $cordovaSQLite.openDB({ name: "my.db" });
不要忘记添加插件:
cordova plugin add cordova-sqlite-storage