Timo.Klement
Timo.Klement

Reputation: 637

How to return a value inside a function to another function?

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?

Upvotes: 0

Views: 111

Answers (4)

Kaken
Kaken

Reputation: 163

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

Upvotes: 1

tymeJV
tymeJV

Reputation: 104785

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

Upvotes: 2

karaxuna
karaxuna

Reputation: 26940

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

Upvotes: 0

Itay Kinnrot
Itay Kinnrot

Reputation: 721

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

Upvotes: 1

Related Questions