Sqlite - Unable to return selected data angularjs service

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.
	}

});

I hope angular experts will be there.

So please try to find out good solution for me. :frowning:

Thank You!

1 Like

this will never work…
your webservice-function executes async code. which is handled in a promise.
$cordovaSQLite.execute(db, query).then(function(res) {

So your function should also return a promise:

obj.getNote = function() {
    var query = "SELECT subject, note FROM notes";
    // return promise!
    return $cordovaSQLite.execute(db, query).then(function (res) {
        if (res.rows.length > 0) {               
            var data = res.rows;
            console.log(data); //Result - Working..
            // Return final data
            return data; //Result - Undefined..
        }
        console.log("No results found");
        // return false if nothing found
        return false;
    });
};

in your controller:

$scope.getNote =  function(){
    webservice.getNote().then(function (data) {
        // data or false
        console.log(data);
    }, function (err) {
        // if failing
        console.log(err)
    });
};

You do not need to create an own promise, because if you return return $cordovaSQLite.execute → you get a promise.
Promises are chainable → so you can simple return your response data instead of calling q.resolve(xxx) or q.reject(err)

2 Likes

@bengtler 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 you tell me How to fix this?

Please refer the image.

This is my controller:

   $scope.getNote =  function(){
      webservice.getNote().then(function(res){ 
            console.log(res); 
            $scope.notes = res;
        },function(err){   
            console.log(err);  
        });
 }

This is my view:

<ion-content id="note" has-bounce="true" >
	    <span ng-repeat = "note in notes | orderBy:'-id' track by $index">
			<div class="card card-border">
				<div class="item item-divider">
				    {{note.subject}}
				</div>
				<div class="item item-text-wrap">
				    {{note.note}}
				</div>
			</div>
		</span>
	</ion-content>

Thanks you!

where do you call $scope.getNote?