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);

        },
Hadrien Delphin
  • 590
  • 1
  • 6
  • 19

1 Answers1

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));
    }
);
Grundy
  • 13,356
  • 3
  • 35
  • 55