0

so far I had no more problems and thought I could handle AngularJS a bit.
But now I tried to return a value without any results.
The third line calls my function to fetch a name from the database. But that function doesn't return the result.

$scope.showModal = function ($event,listId) {
    console.log("showModal: "+$event.type+" - id: "+listId);
    console.log("scope.listname: "+getListname(listId));
    $('#edit').modal('toggle');
};

function getListname(listId) {
    var query = 'SELECT name FROM Lists WHERE id=(?)';
    $scope.db.transaction(
        function (transaction) {
            transaction.executeSql(query, [listId],
                function (tx, results) {
                    // console.log("Result: "+results.rows.item(0).name); // works!
                    return results.rows.item(0).name; // returns nothing or not to the sender
                }                      
            );
        }
    );
}

If I use console.log() within executeSql I get a value in the console. But why can't I get my result back to the calling function?

1

4 Answers 4

2

Welcome to the world of async! executeSql is async so use a callback to access the data once that function completes:

function getListname(listId, callback) {
    var query = 'SELECT name FROM Lists WHERE id=(?)';
    $scope.db.transaction(
      function (transaction) {
        transaction.executeSql(query, [listId],
            function (tx, results) {
                // console.log("Result: "+results.rows.item(0).name); // works!
                callback(results.rows.item(0).name); // returns nothing or not to the sender
            }                      
        );
    }
);

And then call it!

getListName(listId, function(name) {
    console.log(name);
});
Sign up to request clarification or add additional context in comments.

Comments

1
function getListname(listId) {
     var query = 'SELECT name FROM Lists WHERE id=(?)';
     var deferred = $q.defer();
     transaction.executeSql(query, [listId],
            function (tx, results) {
                 deferred.resolve(results.rows.item(0).name);
            }                      
     );
     return deferred.promise;
  }

Can use like this

 getListname(listId).then(function(name){
     $scope.db.transaction = name;
 });

1 Comment

Finally! Thank you ... it works! I just had to change $scope.db.transaction to $scope.listname. My problem was that I tried that deferred but as result I got an object [then, catch, finally] and didn't know what to do with it.
1

You should use promise in order to write the code in sync way but execute it in async. https://docs.angularjs.org/api/ng/service/$q

3 Comments

Uh ... I tried to avoid that promise. In the past I had always problems with that because I don't really understand how to use it or how to implement it e.g. to my queries. :-(
@Tipo: Well, good time to learn more about it :) html5rocks.com/en/tutorials/es6/promises
Thank you for that link. I will look after it.
0

Your request is asynchronous, so you should handle it with callback:

function getListname(listId, callback) {
    var query = 'SELECT name FROM Lists WHERE id=(?)';
    $scope.db.transaction(
        function (transaction) {
            transaction.executeSql(query, [listId],
                function (tx, results) {
                // console.log("Result: "+results.rows.item(0).name); // works!
                    callback(results.rows.item(0).name); // returns nothing or not to the sender
                }                      
            );
        }
    );
}

4 Comments

Sorry, but I got an error. I guess the "callback" is an unknown function. The error is: Uncaught TypeError: undefined is not a function
you shall pass callback function as a parameter to getListname like this: getListName(listId, function(name){ you have name variable here });
@Tipo the second function that you pass, will be called when database query operation will be completed
Hm, let me try what I think you will tell me here: console.log("scope.listname: "+getListname(listId, function (listname) {$scope.listname = listname;})); Ok this don't show me an error but it also don't show me a result. I guess that I don't understand what you mean. I'm really really sorry. :-(

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.