Calling Service from a then() resulting from another service

Asked

Viewed 46 times

0

The situation is as follows from the controller equipecontrol, I’m walking a forEach, inside a then(), which has previously achieved a result by performing the function getEscalacao(), that is defined in service EquipeService, as stated in the code below.

example.controller('equipecontrol', ['EquipeService','PessoasService','$scope','$q', function(EquipeService,PessoasService,$scope,$q) {
    Parse.initialize('APPLICATION_ID', 'JAVASCRIPT_KEY'); 
    Parse.serverURL = 'https://parseapi.back4app.com';

    EquipeService.getEscalacao().then(function (escalacao) {
        var idjogador = '';

        angular.forEach(escalacao, function (atuacao) {
            idjogador = atuacao.attributes['jogador'].id; //id do jogador
            gtStorageJogador = PessoasService.getPessoasStorage(idjogador); //<-- aqui invoco a função do outro service

            console.log(gtStorageJogador) //<-- este console não apresentada nada
        })
    })
}]);

Note in the code above that I get the idjogador in the current iteration, class Escalacao, and I try to research it in class PeopleObject by invoking the function getPessoasStorage() which is defined in another service by name PessoasService, note also that step idjogador as parameter. Follows the code of service PessoasService:

example.service('PessoasService', [function () {
    var jogadorDadosStorage = {};

    return {
        getPessoasStorage: function (params) {
            var PeopleObject = Parse.Object.extend("PeopleObject");
            var PeopleQuery = new Parse.Query(PeopleObject);

            //console.log(params)
            if(params !== undefined) {
                PeopleQuery.equalTo('objectId',params);
            }

            PeopleQuery.find({   
                success: function(pessoas) {
                    for (var i = pessoas.length - 1; i >= 0; i--) {
                        objpessoas = pessoas[i];

                        idpessoa   = objpessoas.id
                        nomepessoa = objpessoas.get('username');
                        posorigem  = objpessoas.attributes.posicaooriginal.id;

                        emailpes   = objpessoas.get('email');
                        senhapes   = objpessoas.get('senha');   
                        dtalter    = objpessoas.get('updatedAt');
                        dtcreate   = objpessoas.get('createdAt');

                        jogadorDadosStorage = { 
                            'id'      : idpessoa,
                            'nome'    : nomepessoa,
                            'posorig' : posorigem,
                            'email'   : emailpes,
                            'senha'   : senhapes,
                            'dtalt'   : dtalter,
                            'dtcria'  : dtcreate
                        }
                    }                
                }, error: function(error) {
                    console.log("Error da Query PeopleObject: " + error.code + " " + error.message)
                    var objSrv = {};
                }
            })

            return jogadorDadosStorage;
        }
    }
}]);

I still don’t know why this logic isn’t working. I put a console.log(gtStorageJogador) but it does not present results, it is as if the function getPessoasStorage was not being executed at this time, I think it may be a problem associated with the issue of asynchrony.

In this section of the program, that is, when going through the forEach for each class item Escalacao, I need a function that can bring me from class PeopleObject the name corresponding to idjogador positioned at this time in the class Escalacao, because my intention at the end of all this is to fill an object containing data from various classes, to be able to display in a view.

Can someone help me?

1 answer

0

How you are utilizing a service of the type service you must return the functions with the declaration this to be accessed in this way:

example.service('PessoasService', [function () {
    var jogadorDadosStorage = {};

    this.getPessoasStorage: function (params) {
        // Sua função aqui

        return jogadorDadosStorage;
    }
}]);

The way you stated it would be for use of a service of the type factory. But try the change I recommended and see if it works.

  • Hello Celsomtrindade, good evening! Thank you for your attention! after implementing this. the system presented the following: "Uncaught Syntaxerror: Unexpected token . "

  • I realized now the following, the getPeopleStorage function is being executed, but only after the completion of the "angular.foreach(escalation, Function(acting)". How can I fix this? , i.e., make getPeopleStorage run while I’m going through each item of the escalation ?

Browser other questions tagged

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