0

i have little problem,

i'm trying to get datas from my SQLite Database, the function is working,

i just have a problem when i need to recovers my datas

this is what i did :

     $scope.facilityDatas = {};


  dataBaseService.getFacilityById($stateParams.facilityId,function (data) {
    $scope.facilityDatas = angular.copy(data);
    console.log ('facility json : '+ angular.toJson($scope.facilityDatas));
  });

the data var contain a data array that contain my request result. i tested in my service, everything is good. now i just need to make $scope.facilityDatas = data but it's not working...

I need to admit that i'm a bit lost. i'm almost sure it's nothing but i don't know what to do...

Hope you'll find what's wrong.

regards

EDIT

here's my DataBase function :

getFacilityById: function(id,callback){

            var data = [];
            $cordovaSQLite.execute(db,'select * from FACILITIES where facilities_id = ?',[id]).then(function (results){
                console.log(angular.toJson(results.rows.item(0)));

                for (var i = 0, max = results.rows.length; i < max; i++) {

                    data.push(results.rows.item(i))

                }

            })



            callback(data);

        },
3
  • Can you add your dataBaseService.getFacilityById method ? that would help to understand the format of your response. Commented Apr 21, 2016 at 15:11
  • Can you make a plunker? Commented Apr 21, 2016 at 15:11
  • Possible duplicate of How do I return the response from an asynchronous call? Commented Apr 21, 2016 at 15:21

1 Answer 1

1

You call callback before $cordovaSQLite.execute finish, so you get empty array.

Move this call inside then

getFacilityById: function(id,callback){
    var data = [];
    $cordovaSQLite.execute(db,'select * from FACILITIES where facilities_id = ?',[id]).then(function (results){
        console.log(angular.toJson(results.rows.item(0)));
        for (var i = 0, max = results.rows.length; i < max; i++) {
            data.push(results.rows.item(i))
        }
        callback(data);
    });
},

Or even return promise and use it directly, like this

getFacilityById: function(id,callback){
    var data = [];
    return $cordovaSQLite.execute(db,'select * from FACILITIES where facilities_id = ?',[id]).then(function (results){
        console.log(angular.toJson(results.rows.item(0)));
        for (var i = 0, max = results.rows.length; i < max; i++) {
            data.push(results.rows.item(i))
        }
        return data;
    });
},

and use it as

$scope.facilityDatas = {};

dataBaseService.getFacilityById($stateParams.facilityId).then(
    function (data) {
        $scope.facilityDatas = angular.copy(data);
        console.log ('facility json : '+ angular.toJson($scope.facilityDatas));
    }
);
Sign up to request clarification or add additional context in comments.

Comments

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.