Invoke Service from a then () resulting from another service

0

The situation is as follows, from controller equipecontrol , I'm traversing a forEach , inside a then() , which has a result obtained previously by executing the getEscalacao() function, which is defined in service EquipeService , as shown 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
        })
    })
}]);

Notice in the code above that I get the idjogador in the current iteration, class Escalacao , and try to search it in the PeopleObject class by invoking the getPessoasStorage() function that is defined in another service name PessoasService , also note that step idjogador as parameter. Here is the code for 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 do not know why this logic is not working. I put console.log(gtStorageJogador) but it does not show results, it's like the getPessoasStorage function was not running at the moment, I think it can be a problem associated with the question of asynchrony.

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

Can anyone help me?

    
asked by anonymous 08.04.2017 / 04:37

1 answer

0

Since you are using a service of type service , you should return the functions with the this declaration to be accessed, like this:

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

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

        return jogadorDadosStorage;
    }
}]);

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

    
08.04.2017 / 15:36