0
votes

I am developing an Ionic app with $cordovaSQLite installed, but when I tried running openDatabase, I got the error "TypeError: Cannot read property 'openDatabase' of undefined".

I double checked my code that the openDatabase call is inside the $ionicPlatform.ready function and the database file has been properly installed, may I have any advices?

Thanks and Regards,

Jimmy

2
Where are you testing your code? On browser? - Davide Pastore
@Davide: I tested on my device directly by the "run android" command - Wonderjimmy
Can you add the section where you call openDatabase function? - Davide Pastore

2 Answers

0
votes

First make sure you have ngCordova properly installed. (install instructions can be found on ngcordova website), along with the named plugin.

When you get undefined error you should always look if you have the dependency defined. That is the most common mistake with people starting with AngularJS.

Example code:

//inject your dependencies;
ControllerOrConfigFunction.$inject = ["$scope", "$cordovaSQLite"]

function ControllerOrConfigFunction($scope,$cordovaSQLite){
//your code here
}

This applies to all functions in which you are using the $cordovaSQLite plugin. You can also find a great example on ngCordova website.

There are multiple ways you can define your Dependency Injection in AngularJs, i recommend it for now, after that take a look at ng-annotate.

0
votes
if (window.cordova) {
    db = $cordovaSQLite.openDB({ name: "my.db" }); //Device
    console.log("Android");
}
else
{
    db = window.openDatabase("my.db", '1', 'my', 1024 * 1024 * 100); // Browser
    console.log("browser");
}