TypeError:无法读取属性“openDatabase”未定义 [英] TypeError: Cannot read property 'openDatabase' of undefined
问题描述
我想第一次使用sqlite与cordova框架。我已经阅读了一个教程,我应该使用ngcordova像这样:
I want to work with sqlite with cordova framework for the first time. As I've read on a tutorial I should use ngcordova like this:
var db = null;
app.controller('mainCtrl', function ($scope, $ionicSideMenuDelegate) {
$scope.toggleLeft = function () {
$ionicSideMenuDelegate.toggleLeft();
}
$scope.toggleRight = function () {
$ionicSideMenuDelegate.toggleRight();
}
})
.controller('home_ctrl', function ($scope, $cordovaSQLite) {
db = $cordovaSQLite.openDB({name: "my.db"});
//db = $window.opendb({name: "my.db"});
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS people(id integer primary key, firstname text, lastname text)")
})
;
当我运行此代码时出现错误:
When I run this code an error says:
TypeError: Cannot read property 'openDatabase' of undefined
In some articles like this: How do I use the ngCordova sqlite service and the Cordova-SQLitePlugin with Ionic Framework?
recommends to use commands like this: ionic start myApp sidemenu
I don't know what is it, I just use cordova command like: cordova run android
or cordova create my_project
What should I do to run my create a table from my cordova project?
推荐答案
您可以参考本教程:
You might take a look at this tutorial:
https://www.thepolyglotdeveloper.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/
当您看到此错误:
TypeError:无法读取属性openDatabaseundefined
TypeError: Cannot read property 'openDatabase' of undefined
这是由于以下原因之一:
It is happening for one of a few reasons:
- 您不在
$ ionicPlatform.ready()
函数中包装$ cordovaSQLite
- 您尝试从网络浏览器测试此原生插件。
- 您尚未在项目中实际安装基本SQLite插件。
- You are not wrapping the
$cordovaSQLite
methods in the$ionicPlatform.ready()
function. - You are trying to test this native plugin from a web browser.
- You have not actually installed the base SQLite plugin into your project.
此错误的最常见原因是#1和#2。原生插件,必须在应用程序确认准备就绪后使用,因此 $ ionicPlatform.ready()
方法。由于本机插件使用本机代码,因此您无法通过Web浏览器测试它们。
The most common reasons for this error are #1 and #2. Native plugins, must be used only after the application is confirmed ready, thus the $ionicPlatform.ready()
method. Since native plugins use native code, you cannot test them from your web browser.
阅读我链接的教程,因为它可以帮助您。
Read through the tutorial I linked because it should help you.
请注意,
这篇关于TypeError:无法读取属性“openDatabase”未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!