R0b0tn1k
R0b0tn1k

Reputation: 4306

Angular: Uncaught ReferenceError: $cordovaSQLite is not defined

Trying to follow an example for using SQL lite, but hitting a stump.

Example says, include ngCordova in app.js as such:

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ngCordova'])

That part works.

Then create the DB + table in .run directive:

    db = $cordovaSQLite.openDB("space.db");
    $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS locations (id integer primary key, latitude text, longitude text, altitude text, speed text, date text, time text)");

That part works as well...

Then, in services.js i want to add a record when i get the GPS location.

I do that using this code in services.js:

angular.module('starter.services', [])
.factory('Geolocation', function() {

  var callBackFunction ;
  var latitude, longitude, altitude ;

  function getPosition(){
      return new Array(latitude, longitude, altitude, speed, GPSEnabled);
  }

    function showPosition(position) {

      latitude = position.coords.latitude ;
      longitude = position.coords.longitude ;
      altitude = position.coords.altitude ;
        speed = position.coords.speed ;


      var query = "INSERT INTO locations (latitude, longitude, altitude, speed) VALUES (?,?,?,?)";
      $cordovaSQLite.execute(db, query, [latitude, longitude, altitude, speed, date, time]).then(function(res) {
          console.log("INSERT ID -> " + res.insertId);
      }, function (err) {
          console.error(err);
      });

      callBackFunction(); 
    }

     navigator.geolocation.getCurrentPosition(showPosition, showError, options);

}

However, I get the error

Uncaught ReferenceError: $cordovaSQLite is not defined

How does services.js inherit $cordovaSQLite that's defined in app.js?

Upvotes: 0

Views: 1443

Answers (1)

JanS
JanS

Reputation: 2075

Inject the $cordovaSQLite service into your factory like this:

angular.module('starter.services', [])
.factory('Geolocation', ['$cordovaSQLite', function($cordovaSQLite) {
    //do something with $cordovaSQLite
}]);

Here is a little more on the syntax I used: https://docs.angularjs.org/guide/di#dependency-annotation

edit: Your module starter.services might also be missing the cordova module ngCordova: angular.module('starter.services', ['ngCordova'])

Upvotes: 3

Related Questions