S
S
Seva2015-07-28 22:03:19
Angular
Seva, 2015-07-28 22:03:19

Why does the browser give an error?

services.js

angular.module('events.services', [])
.factory('EventService', function($http, $cordovaSQLite) {
    return {
        getDataFromDB: function() {
            var query = 'SELECT id, title, img, coords, details, dateString, timestamp FROM events ORDER BY timestamp DESC';
            $cordovaSQLite.execute(db, query).then(function(response) {
                result = response;
                return result;
            });
        }
    }
})

controllers.js
angular.module('events.controllers', ['events.services'])
.controller('NearCtrl', function($scope, $http, $cordovaSQLite, EventService) {
    EventService.getDataFromDB().then(function(result) {
        console.log(result);
    });
}

At the output I get the error "TypeError: Cannot read property 'then' of undefined"
But! If you push the same GetDataFromDB function into the controller, then everything works fine.
What am I doing wrong? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2015-07-28
@miraage

angular.module('events.services', [])
.factory('EventService', function($http, $cordovaSQLite) {
    return {
        getDataFromDB: function() {
            var query = 'SELECT id, title, img, coords, details, dateString, timestamp FROM events ORDER BY timestamp DESC';
            return $cordovaSQLite.execute(db, query);
        }
    }
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question