0

I got this array objects to be read:

enter image description here

These was my sample codes:

$scope.obj_qst_local_study_main = tbl_qst_local_study_main.all();
$scope.quesion_id_allocated = $scope.obj_qst_local_study_main[0];
$timeout(function(){
    console.log('----------all objects----------');
    console.log($scope.obj_qst_local_study_main);
    console.log('-----------one object-----------');
    console.log($scope.quesion_id_allocated);
},200);

When I used:

$scope.obj_qst_local_study_main[0];

The result was: undefined


My angularjs services:

.service('tbl_qst_local_study_main', function($cordovaSQLite, DATABASE_LOCAL_NAME){
            var self = this;
            var qst_local_study_main_array = [];
            self.all = function() {
              var db = $cordovaSQLite.openDB({name: DATABASE_LOCAL_NAME,location:'default'});
                    $cordovaSQLite.execute(db, "SELECT * FROM qst_local_study_main")
                            .then(function (res) {
                                console.log('--------Successfully read from qst_local_study_main---------');
                                for (var i = 0; i < res.rows.length; i++) {
                                    qst_local_study_main_array.push(res.rows.item(i));
                                }
                            },
                                function (err) {
                                    console.log(err);
                                });
                    return qst_local_study_main_array;
            };
        })
18
  • 1
    You need to show some code. For exmple, what does tbl_qst_local_study_main.all(); do? Commented Oct 24, 2016 at 7:17
  • So my wild guess is that tbl_qst_local_study_main.all() is an async call. so, when you assign the first item in the second line the call has not finished. Commented Oct 24, 2016 at 7:20
  • A wild asynchronous function that returns a value appears. Commented Oct 24, 2016 at 7:22
  • So what I need to do then @SebastianSebald ? Commented Oct 24, 2016 at 7:23
  • put $scope.quesion_id_allocated = $scope.obj_qst_local_study_main[0]; inside the $timeout Commented Oct 24, 2016 at 7:23

1 Answer 1

4

Your service should return a Promise. This is a super common case, because (don't be offended please) people do not understand how Promises work.

Please search the internet for an article, like this one: https://developers.google.com/web/fundamentals/getting-started/primers/promises

tl;dr Your service should return a Promise. In your case $cordovaSQLite.execute Then you can correctly handle the response by chaining thens. You also do not need the timeout. Using a timeout is super bad here!

tbl_qst_local_study_main.all()
  .then(function(result) {
    console.log(result);
  })
Sign up to request clarification or add additional context in comments.

5 Comments

$timeout was super bad?Oh no..so I need to use promise..I need to try first.
Yes, because you can not predict how long you async call needs. Your code would throw/return nothing if the call needs more than 200ms. :-(
Can you show me? I do not know how to use promise
Added an example. But please go and read an article about Promises. Not going to write you your software ;-)
Ok..I got it. Then I should remove all timeout I used to get the returned value

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.