0

In my ionic app. I created a service named webservice to retrieve data from sqlite database using angularjs.It selecting data properly and get result in console it works fine, but it return undefined always.

This is my sample code:

app.factory('webservice',function($http,$cordovaSQLite,$q){

    var obj = {};

    obj.getNote =  function(){
        var query = "SELECT subject, note FROM notes";
        var q = $q.defer();
        $cordovaSQLite.execute(db, query).then(function(res) {
            if(res.rows.length > 0) {               
                var data = res.rows;
                console.log(data); //Result - Working..
                q.resolve(res.rows); //Result - Undefined..
                return data; //Result - Undefined..
            } else {
                console.log("No results found");
                return false;
            }
        }, function (err) {
            console.error(err);
        });
    }

    return obj;

});

This is my controller:

app.controller('noteCtrl', function($scope,$state,webservice){

    $scope.getNote =  function(){
        console.log(webservice.getNote()); // Undefined.
    }

});

So please try to find out good solution for me.

Thank You!

1 Answer 1

4

Two things. First you're not returning the promise object from your service, second, you are not calling the then function within the controller. Try the following code:

app.factory('webservice',function($http,$cordovaSQLite,$q){

    var obj = {};

    obj.getNote =  function(){
        var query = "SELECT subject, note FROM notes";
        var q = $q.defer(); //this is your deferred object
        $cordovaSQLite.execute(db, query).then(function(res) {
            if(res.rows.length > 0) {               
                var data = res.rows;
                console.log(data); //Result - Working..
                q.resolve(res.rows); //Result - Undefined..
                return data; //Result - Undefined..
            } else {
                console.log("No results found");
                q.reject("No results found"); 
                return false;
            }
        }, function (err) {
            q.reject(err);
            console.error(err);
        });

        return q.promise;       //This line was missing

    }

    return obj;

});

and in controller do this :

 app.controller('noteCtrl', function($scope,$state,webservice){

    $scope.getNote =  function(){
        webservice.getNote().then(
            function(res){   //success callback
             console.log(res);  //this should be the resolved object
            },
            function(err){   //error callback
             console.log(err);  //this should be the rejected object
            });
    }

});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it works. But when i use ng-repeat in view i get the below error [$rootScope:infdig] 10 $digest() iterations reached. Aborting! Could your tell me How to fix this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.