-1

I am trying to retrieve data from a SQL database and wrote the following function:

$scope.sqlRequest = function (Req) {
            console.log("Main.js requesting from DB :", Req)
            return server_operations.sqlRequest([Req])
            .success(function (Results) {
                console.log("Results returned to .success = ", Results);
                return Results.data
            });
        }

When I run the function the results are displayed correctly in the console. But when I try to use the data in any further context like:

Res = $scope.sqlRequest("<Some SQL String here>")

the variable Res will always be undefined. What do I have to change to get the data into a global variable ?

Marcus Höglund
  • 13,944
  • 9
  • 42
  • 63
Ente Fetz
  • 323
  • 1
  • 2
  • 10
  • Are you printing the value of `Res` on the success callback, or outside the entire function? – J. Chen Jul 19 '17 at 17:22
  • 3
    `.sucess` or should be `.success` (two c's)? – JM-AGMS Jul 19 '17 at 17:23
  • I want to display the content of Res in a drop-down menu, which means the data must be accessible by the HTML file, which calls the function, but Res is always undefined. – Ente Fetz Jul 19 '17 at 17:31
  • First inject the $q to your controller, then use it to create promises $scope.sqlRequest = function (Req) { var deferred = $q.defer(); console.log("Main.js requesting from DB :", Req) server_operations.sqlRequest([Req]) .success(function (Results) { console.log("Results returned to .success = ", Results); deferred.resolve(Results.data); }). error(function (message, status) { deferred.reject(message, status); }); return deferred.promise; }; call the $scope.sqlRequest() $scope.sqlRequest("").then(function(data){ Res = data; }); – Marcus Höglund Jul 19 '17 at 18:05

1 Answers1

-1

you need to use the response returned in the callback function because it is async. var myVar = myAsycnFunction(); will always be undefined as no response will have been returned from the database call when it is evaluated. Instead, try

.success(function( results ){
    processResults( results );
});

where processResults() is a function with logic for handling whatever you want to do with the results.

Edit to clarify: if you only ever want to use one function to process the results, you can just call it in the success function, passing in your results data. If you want to be more flexible, you can pass in a callback function to invoke in your success function and pass in your results data, as in the example below.

$scope.sqlRequest = function( req, callback ) {
  server_operations.sqlRequest( req )
    .success( function( res ) {
      // invoke the callback function we pass in to process results from the request
      callback( res.data );
    });
}

function processData( data ){
  // I do something with the data
}

// call your data fn and pass in a callback to process the results
$scope.sqlRequest( req, processData );
sauntimo
  • 1,129
  • 1
  • 14
  • 21
  • The problem is, that I have several functions, which will call this request function and what I want to do with the data depends on which function called the request. That means the processResults function would have to know which function called the request function in the first place. This does not seem practicable to me. – Ente Fetz Jul 19 '17 at 17:35
  • you can specify what function you want to use as your call back when you call the original function `function( sql, callback ){ //code }).success( function( results, callback ){ callback( results ) } );` ie, you call whatever callback function you pass in with the results you've got as a parameter. That's a pretty common pattern. – sauntimo Jul 19 '17 at 17:36
  • Ok, I now solved the problem by using a callback and it now looks like this: $scope.sqlRequest = function (Req, callback) { console.log("Main.js requesting from DB :", Req) server_operations.sqlRequest([Req]) .success(function (Results) { console.log("Results returned to main.js = ", Results.rows); callback(Results); }); } – Ente Fetz Aug 02 '17 at 15:17
  • @EnteFetz that sounds good to me :) Not sure why I got down voted, but I'm glad that you're making progress now. – sauntimo Aug 02 '17 at 15:51
  • @sautimo I fist did not understand what you wanted to say with your first answer, especially, becasue the code you provided in your original answer did not clarify, that "processResults" is a function, that has to be given to the async function as an argument. What I was locking for was something like this and an explanation how it works: sqlRequest = function (Req, callbackfunction) { server_operations.sqlRequest(Req) .success(function( results ){ callbackfunction( results ); }); } If you extend your original answer a bit like that, then I can mark it as accepted ;) – Ente Fetz Aug 03 '17 at 11:55
  • I've updated my answer now – sauntimo Aug 03 '17 at 12:08