Take value from within a function

Asked

Viewed 514 times

3

I have a logic problem here, I think it must be a very "dumb" thing on my part, like, I have to take a value from inside the function, and I’m not getting with a Return because it’s an http request. look:

$scope.postData = function(method, data, file){

    var uploadUrl = base_url+'main/upload/';

    fileUpload.uploadFileToUrl(file.file, uploadUrl, file.title);

    $http.get(base_url+'main/lastId/')
    .success(function (data) {
        lastId = data;
        return lastId; // <------ PRECISO DESSA VARIÁVEL
    });

    data.last_id = lastId; // <----- PARA USAR ELA AQUI

    $http.post(base_url+method+'/post/'+table, data)
    .success(function (data) {
        $scope.posts = data;
        $scope.exibirForm = 'listar';
        $scope.form = {};
        $scope.estadoBotao = "Adicionar";
    });
}

as you have seen, I need to take lastId, to modify the data array to make a post with a file id link.

  • You have to make a callback as the answers below.

2 answers

4


The http Angularjs is asynchronous, so the variable will be undefined at this time.

One option is to use it in success, something like this:

$http.get(base_url + 'main/lastId/')
    .success(function(data) {
        lastId = data;
        facaAlgo(lastId); // irá passar o valor para a função
    });


function facaAlgo(lastId) {

    data.last_id = lastId; // Aqui poderá usar lastId, porquê terá sido resolvido pela Promise

    $http.post(base_url + method + '/post/' + table, data)
        .success(function(data) {
            $scope.posts = data;
            $scope.exibirForm = 'listar';
            $scope.form = {};
            $scope.estadoBotao = "Adicionar";
        });
}

What happens is that you are trying to use a value that does not yet exist, because the http will not halt execution, it will be executed and move to the next statement. The name to resolve this type of situation is callback.

  • 1

    Thanks man, it worked. It helped me understand. This part of $http.get being asynchronous was getting in my way, because I didn’t know this kk.

  • Dude, there was another problem q n had noticed before... the fileUpload.uploadFileToUrl() function; it also has http.post. And it’s this post I want to take the lastId, so that’s why you’re getting a previous id from the last one. And how I made a it as a service, how do I obey a Promise?

  • 1

    Depending on the form of your implementation you can return to Promise and resolve it elsewhere @Viníciusvilela. If you still have this problem I recommend asking another question with the details :)

1

You can do a CALLBACK, passing the parameter to a new method, because when the AJAX result arrives, the function/method has probably already been executed.

Below is an example (not tested):

$scope.postData = function(method, data, file){
    var uploadUrl = base_url+'main/upload/';
    fileUpload.uploadFileToUrl(file.file, uploadUrl, file.title);
    $http.get(base_url+'main/lastId/')
    .success(function (data) {
        pegaRetorno(data); // <------ PRECISO DESSA VARIÁVEL
    });
}

function pegaRetorno(lastId){
    console.log(lastId);
    data.last_id = lastId; // <----- PARA USAR ELA AQUI  
    $http.post(base_url+method+'/post/'+table, data)
    .success(function (data) {
        $scope.posts = data;
        $scope.exibirForm = 'listar';
        $scope.form = {};
        $scope.estadoBotao = "Adicionar";
    });
    return valor;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.