Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read property 'openDatabase' of undefined

Tags:

sqlite

cordova

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?

like image 777
ehsan shirzadi Avatar asked Jan 30 '15 20:01

ehsan shirzadi


1 Answers

You might take a look at this tutorial:

https://www.thepolyglotdeveloper.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/

When you see this error:

TypeError: Cannot read property 'openDatabase' of undefined

It is happening for one of a few reasons:

  1. You are not wrapping the $cordovaSQLite methods in the $ionicPlatform.ready() function.
  2. You are trying to test this native plugin from a web browser.
  3. You have not actually installed the base SQLite plugin into your project.

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.

Regards,

like image 114
Nic Raboy Avatar answered Oct 22 '22 14:10

Nic Raboy