我刚刚开始学习Cordova,目前正在使用https://www.toptal.com/mobile/developing-mobile-applications-with-apache-cordova学习SQLite。但是,我的新Cordova项目出现以下错误。我已经粘贴了发生错误的示例代码,但我不知道并且无法找到问题,因为我无法在代码中找到问题。我还为cordova添加了正确的SQlite插件。我很乐意听取您的意见。
Uncaught TypeError: Cannot read property 'openDatabase' of undefined
at new SQLiteStorageService (SQLiteStorageService.js:3)
at Object.initialize (Controller.js:13)
at new Controller (Controller.js:68)
at Object.onDeviceReady (index.js:33)
at Object.initialize (index.js:27)
at index.js:36
Controller.js
var Controller = function() {
var controller = {
self: null,
initialize: function() {
self = this;
new SQLiteStorageService().done(function(service){
self.storageService = service;
self.bindEvents();
//render search view after storage service is initialized.
self.renderSearchView();
}).fail(function(error){
alert(error);
});
},
bindEvents: function() {
$('.tab-button').on('click', this.onTabClick);
},
onTabClick: function(e) {
e.preventDefault();
if ($(this).hasClass('active')) {
return;
}
var tab = $(this).data('tab');
if (tab === '#add-tab') {
self.renderPostView();
} else {
self.renderSearchView();
}
},
renderPostView: function() {
$('.tab-button').removeClass('active');
$('#post-tab-button').addClass('active');
var $tab = $('#tab-content');
$tab.empty();
$("#tab-content").load("./views/post-project-view.html", function(data) {
$('#tab-content').find('#post-project-form').on('submit', self.postProject);
});
},
renderSearchView: function() {
$('.tab-button').removeClass('active');
$('#search-tab-button').addClass('active');
var $tab = $('#tab-content');
$tab.empty();
var $projectTemplate = null;
$("#tab-content").load("./views/search-project-view.html", function(data) {
$projectTemplate = $('.project').remove();
// Load projects here
});
}
}
controller.initialize();
return controller;
}
SQLiteStorageService.js
SQLiteStorageService = function () {
var service = {};
var db = window.sqlitePlugin.openDatabase({name: "demo.toptal",location:"default"});
service.initialize = function() {
var deferred = $.Deferred();
db.transaction(function(tx) {
tx.executeSql(
'CREATE TABLE IF NOT EXISTS projects ' +
'(id integer primary key, name text, company text, description text, latitude real, longitude real)'
,[], function(tx, res) {
tx.executeSql('DELETE FROM projects', [], function(tx, res) {
deferred.resolve(service);
}, function(tx, res) {
deferred.reject('Error initializing database');
});
}, function(tx, res) {
deferred.reject('Error initializing database');
});
});
return deferred.promise();
}
service.getProjects = function() {
var deferred = $.Deferred();
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM projects', [], function(tx, res) {
var projects = [];
console.log(res.rows.length);
for(var i = 0; i < res.rows.length; i++) {
var project = { name: res.rows.item(i).name, company: res.rows.item(i).company, description: res.rows.item(i).description };
if (res.rows.item(i).latitude && res.rows.item(i).longitude) {
project.location = {
latitude: res.rows.item(i).latitude,
longitude: res.rows.item(i).longitude
}
}
projects.push(project);
}
deferred.resolve(projects);
}, function(e) {
deferred.reject(e);
});
});
return deferred.promise();
}
service.addProject = function(name, company, description, addLocation) {
var deferred = $.Deferred();
console.log(db);
if (addLocation) {
navigator.geolocation.getCurrentPosition (
function(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
db.transaction(
function(tx) {
tx.executeSql('INSERT INTO projects (name, company, description, latitude, longitude) VALUES (?,?,?,?,?)',
[name, company, description, lat, lon],
function(tx, res)
{
console.log('success');
deferred.resolve();
}, function(e)
{
console.log('failure');
deferred.reject('Error posting a new project');
});
},
function() {
deferred.reject('Error during save process. ');
}
);
},
function() {
deferred.reject(
'We could not fetch your current location. ' +
'Please try again or post a project without adding a location');
},
{maximumAge: 60000, timeout: 5000, enableHighAccuracy: true}
);
} else {
db.transaction(function(tx) {
tx.executeSql('INSERT INTO projects (name, company, description) VALUES (?,?,?)', [name, company, description], function(tx, res) {
deferred.resolve();
}, function(e) {
deferred.reject(e);
});
});
}
return deferred.promise();
}
return service.initialize();
}
答案 0 :(得分:0)
似乎插件未添加到您的项目中。您可以在项目的根文件夹中执行以下命令并添加插件
cordova plugin add https://github.com/litehelpers/Cordova-sqlite-storage.git
这会将插件添加到您的cordova项目中,并使脚本中的包装器对象可用。有关详细信息,请访问ngcordova。