Hello community,
I’m trying over 5 hours and trying any possible suggestion, but nothing works.
I’m trying to create a service for database operations but I could not call functions in my controller via the DBA service. It returns “TypeError: Cannot read property ‘execute’ of undefined”. I tried to call $q.defer() in my service function. It’s also returning same error for defer function.
Also I’m injecting DBA service in my controller and including the ngCordova.min.js file before cordova.js in index.html.
Can anyone help me to solve that problem?
angular.module('app.services', [])
.service('DBA', [function ($cordovaSQLite) {
function _getKeyValue(key) {
$cordovaSQLite.execute(db, 'SELECT value FROM ThingConfigs WHERE key=?', [key]).then(
function (result) {
console.log(key + " select result: " + JSON.stringify(result));
if (result.rows.length > 0)
console.log(key + " value: " + result.rows.item(0).value);
return result.rows.item(0).value;
},
function (error) {
console.log(key + " select error: " + JSON.stringify(err));
return null;
}
);
}
return {
getKeyValue: function(key) {
return _getKeyValue(key);
}
};
}])
.controller('addThingCtrl', ['$rootScope', '$scope', '$stateParams', '$cordovaSQLite', 'ThingConfigService', 'DBA',
function ($rootScope, $scope, $stateParams, $cordovaSQLite, ThingConfigService, DBA) {
$scope.formData = {};
$scope.formData.KEY_SSID = "ALPs";
$scope.formData.KEY_NODEID = DBA.getKeyValue(KEY_NODEID);
// TODO: Set formData default values in for each loop (key in KEYS).
//Go to the guessing page
$scope.discover = function(){
/*
console.log($scope.formData.ssid);
console.log($scope.formData.password);
console.log($scope.formData.nodeId);
*/
};
}])
And I’m getting this error output on device;
OPEN database: my.db
SQLitePlugin.js:106 new transaction is waiting for open operation
SQLitePlugin.js:179 OPEN database: my.db - OK
SQLitePlugin.js:80 DB opened: my.db
app.js:51 Create table error: {"message":"sqlite3_prepare_v2 failure: table ThingConfigs already exists","code":0}
app.js:56 ssid select result: {"rows":{"length":1},"rowsAffected":0}
app.js:58 ssid value: ALP
app.js:56 ssid_pass select result: {"rows":{"length":1},"rowsAffected":0}
app.js:58 ssid_pass value: 12345
app.js:56 node_id select result: {"rows":{"length":1},"rowsAffected":0}
app.js:58 node_id value: NodeAlp
ionic.bundle.js:25642 TypeError: Cannot read property 'execute' of undefined
at _getKeyValue (services.js:18)
at Object.getKeyValue (services.js:42)
at new <anonymous> (controllers.js:62)
at invoke (ionic.bundle.js:17762)
at Object.instantiate (ionic.bundle.js:17770)
at ionic.bundle.js:22326
at self.appendViewElement (ionic.bundle.js:56883)
at Object.render (ionic.bundle.js:54995)
at Object.init (ionic.bundle.js:54915)
at self.render (ionic.bundle.js:56743)(anonymous function) @ ionic.bundle.js:25642(anonymous function) @ ionic.bundle.js:22421$broadcast @ ionic.bundle.js:29479$state.transition.resolved.then.$state.transition @ ionic.bundle.js:49321processQueue @ ionic.bundle.js:27879(anonymous function) @ ionic.bundle.js:27895$eval @ ionic.bundle.js:29158$digest @ ionic.bundle.js:28969$apply @ ionic.bundle.js:29263(anonymous function) @ ionic.bundle.js:31030completeOutstandingRequest @ ionic.bundle.js:18706(anonymous function) @ ionic.bundle.js:18978