0

I have an array with objects each of these Objects contains information for my menu. In the Image u can see that one of these Objects contains: id, lang, link, modul_id,shrtModul, sort.

Now my problem is that

when I console.log($scope.usrModules) and click on the 3rd Object

I see the key sort with the value 1.

but when i console.log($scope.usrModules[2]);

there is all information, however the sort key and value are missing somehow.

and when i console.log($scope.usrModules[2].sort);

the log returns undefined.

I am not really sure why this is happening, but maybe someone can help me there.

Since i cant post Images here a link myProblem

My Controller

$scope.usrModules = ParameterService.setMenu(Ls.getObject('mandants'), Ls.getObject('mandantId'));
            console.log($scope.usrModules);
            console.log($scope.usrModules[2]);
            console.log($scope.usrModules[2].sort);

This is my Service where i build the array of Objects at the end i return arrModules which is the $scope.usrModules.

.factory('ParameterService', function ($http, Ls, $location, $filter, CommonService) {
var arrModules;
var modulesOrder = [];
var unique = {};

return {
    getAllDamage: function (callback) {
        $http.get(Ls.get('persist_ipAddress') + "ExternalParameter/GetAllParameters?businessUnitId=" + Ls.getObject("mandantId"))
            .success(callback);
    },
    // does load the specific modules the user is allowed to use
    setMenu: function (mandants, mandantId) {
        // array of language keys which are used in this view !IMPORTANT -> needs to be in the ascending order as in the language object
        var arrLang = ['btnDatenLadenML', 'btnDatenSendenML', 'btnMenuABML', 'btnMenuALML', 'btnMenuBLML', 'btnMenuELML', 'btnMenuGUML', 'btnMenuHMML', 'btnMenuIVML', 'btnMenuKOML', 'btnMenuMAML', 'btnMenuMIML', 'btnMenuSTML', 'btnMenuULML', 'btnMenuVEML', 'btnMenuVPML', 'btnMenuVSML', 'btnMenuWEML', 'btnTourStartML'];
        var lang = CommonService.setLang(arrLang);

        arrModules = [];
        // arr for nested modules 'versand' and ablieferung
        arrInnerModules = [];
        arrInnerModulesAB = [];

        unique = {};
        // bool to check if module is already added to the menu
        var alreadyAdded = true;

        // loop through all mandants, loop through 
        angular.forEach(mandants, function (value, index) {
            if (value.MandantId == mandantId) { 
                angular.forEach(value.Rechte, function (value, index) {
                    if (value.option_id === 0) {
                        var shrt = value.bezeichnung.split("_").pop();
                        // if the logged in user is an admin (short AM) set a flag
                        if (shrt == 'AM') {
                            Ls.set('admin', true);
                        }
                        // remove duplicate entries in the modules
                        if (!unique[value.id]) {
                            // do not push admin and handheld exit , ablieferung and beladen, verpacken, versandeinheit auflösen modules 
                            if (shrt != 'HE' && shrt != 'AM' && (shrt != 'BL' && shrt != 'VP' && shrt != 'VE') && shrt != 'AB') {
                                arrModules.push({ id: value.id, modul_id: value.modul_id, shrtModul: shrt, link: '#/' + $filter('lowercase')(shrt) + '10' });
                                unique[value.id] = value.id;
                            }

                            // check for the ABLIEFERUNG module and remove the link - add hardcoded the submenue entries as this are no modules -> ID is just any number
                            if (shrt == 'AB') {
                                arrModules.push({ id: value.id, modul_id: value.modul_id, shrtModul: shrt, link: '' });
                                arrInnerModulesAB.push(
                                    { id: '991', modul_id: '14', shrtModul: 'TS', link: '#/ab10', lang: lang.btnTourStartML },
                                    { id: '992', modul_id: '14', shrtModul: 'DL', link: '#/ab60', lang: lang.btnDatenLadenML },
                                    { id: '993', modul_id: '14', shrtModul: 'DS', link: '#/ab70', lang: lang.btnDatenSendenML });
                            }

                            // check if one of the three VERSAND modules are active and create a new menu icon
                            if ((shrt == 'BL' || shrt == 'VP' || shrt == 'VE')) {
                                // hardcoded versand module as it is just a overmenu entry for the beladen, verpacken and versandeinheit auflösen
                                if (alreadyAdded) {
                                    arrModules.push({ id: '99', modul_id: '18', shrtModul: 'VS' });
                                    alreadyAdded = false;
                                }
                                // push the nested modules in a separate array (beladen, verpacken, versandeinheit auflösen)
                                arrInnerModules.push({ id: value.id, modul_id: value.modul_id, shrtModul: shrt, link: '#/' + $filter('lowercase')(shrt) + '10' });
                            }
                            // add the inner modules to the first hierachy
                            for (var i = 0; i < arrModules.length; i++) {
                                if (arrModules[i].shrtModul == 'AB') {
                                    arrModules[i].modules = arrInnerModulesAB;
                                }
                                if (arrModules[i].shrtModul == 'VS') {
                                    arrModules[i].modules = arrInnerModules;
                                }
                            }
                        }
                    }
                })
            }
        })

        $http.get(Ls.get('persist_ipAddress') + "ExternalParameter/GetValues?businessUnitId=" + mandantId + "&parameter=Menu")
         .success(function (data) {
             angular.forEach(data.Result, function (value, index) {
                 modulesOrder.push({ 'sort': value.Sort, 'id': value.ID });
             });
             // bring together the modules and the module Order
             angular.forEach(arrModules, function (valueA, indexA) {
                 angular.forEach(modulesOrder, function (valueMO, indexMO) {
                     if (valueA.modul_id == valueMO.id.slice(-2)) {

                         valueA.sort = valueMO.sort;

                     }
                     // check if there are inner modules and add the sort to the inner modules
                     if (valueA.modules){
                         for (var i = 0; i < valueA.modules.length; i++) {
                             if (valueA.modules[i].modul_id == valueMO.id.slice(-2)) {
                                 valueA.modules[i].sort = valueMO.sort;
                             }
                         }
                     }
                 })
             })
         })

        // bring together the module menue and the language files
        angular.forEach(lang, function (value, index) {
            if (value.indexOf('btn')) {
                var tmpM = index.slice(-4);
                var shortM = tmpM.slice(0, 2);
            }
            angular.forEach(arrModules, function (value2, index2) {
                if (value2.shrtModul == shortM) {
                    value2['lang'] = value;

                }
                // check if there are inner modules and add the language to the inner modules
                if (value2.modules) {
                    for (var i = 0; i < value2.modules.length; i++) {
                        if (value2.modules[i].shrtModul == shortM) {
                            value2.modules[i].lang = value;
                        }
                    }
                }
            });
        });
        return arrModules;
    }
};
})

I already tried to change the Controller to wait for the $http request without an luck

$scope.usrModules = function () {

                ParameterService.setMenu(Ls.getObject('mandants'), Ls.getObject('mandantId'))
                .success(function (arrModules) {
                    var bla = arrModules;
                    console.log('yea' + bla);
                })
                .error(function () {

                    console.log('NOT');
                })
            }

Or This...

$scope.usrModules = function () {

                ParameterService.setMenu(Ls.getObject('mandants'), Ls.getObject('mandantId'))
                .then(function (httpData) {
                    console.log(httpData.data);
                }, function (httpData) {
                    console.log('albums retrieval failed.');
                });
            }
stackg91
  • 584
  • 7
  • 25
  • Are you sure that your model hasn't changed in between your calls to console.log? Maybe someone changed the values in a view? Normally this shouldn't happen. – devnull69 Apr 15 '15 at 09:35
  • Don't use `console.log` to debug - use your browser's debugger instead(e.g. https://developer.chrome.com/devtools/docs/javascript-debugging). The most likely situation is that you modify `usrModules` after trying to print `$scope.usrModules[2].sort`. Most browser's `console.log()` prints values asynchronously(see http://stackoverflow.com/questions/23392111/console-log-async-or-sync). – Sacho Apr 15 '15 at 09:36
  • The thing is i am creating those Objects by doing multiple foreach beacause i get the the Information from different sources, but im not really sure why the first console log shows sort and the ones after dont, im not manipulating or removing data from the objects after they are constructed – stackg91 Apr 15 '15 at 09:40
  • Okay I removed everything which could change the Object, and I noticed, that when i load the page and open the console after and i open the single object($scope.usrModules[2]) the sort value is showing? this must be a bug or something, anyhow the $scope.usrModules[2].sort is still undefined – stackg91 Apr 15 '15 at 09:46
  • My first guess was that somehow the $http request where i get the sort values is running async and thats the reason why sort is sometimes there and sometimes not but I am not sure – stackg91 Apr 15 '15 at 09:55
  • `arrModules.push({ id: value.id, modul_id: value.modul_id, shrtModul: shrt, link: '' });` you add object without `sort` property – Grundy Apr 15 '15 at 09:57
  • I am adding sort later after the $http request – stackg91 Apr 15 '15 at 10:00
  • so all simple: as say above console log try show last info, and when you do `console.log($scope.usrModules);` showed reference to array, and on expand showing last array condition, after _$http_, in other case you see instant value, before _$http_ success – Grundy Apr 15 '15 at 10:10
  • So ur basicly saying i have to wait for the $http to complete before i return the arrModules right? – stackg91 Apr 15 '15 at 10:29
  • it depends on how you use this array – Grundy Apr 15 '15 at 10:42
  • @stackg91 after editing your function return nothing, so `usrModules` always _undefined_ – Grundy Apr 15 '15 at 10:53
  • @stackg91 what problem you try solve? your view show incorrect data? or just error info in console.log? – Grundy Apr 15 '15 at 10:54
  • The problem is i have to save this array to the Localstorage, in case the user has no connection he can login offline and the Menu behaves as he would be online and gets sorted correctly. I want to save the array with the sort in the Localstorage basicly – stackg91 Apr 15 '15 at 10:56
  • Currently when i save the array to LocalStorage the sort is missing so the offline Menu isnt sorted correctly – stackg91 Apr 15 '15 at 10:58
  • @stackg91 so just do it in `success` function _$http_ in your factory – Grundy Apr 15 '15 at 10:58
  • Thanks havent thought about that -.- – stackg91 Apr 15 '15 at 11:05
  • @Grundy Thank you very much Grundy working like a charm =) – stackg91 Apr 15 '15 at 11:15

0 Answers0