简体   繁体   中英

JSON object TypeError: Cannot set property '0' of undefined

I have two JSON objects one that holds the Buildings and the other that holds the rooms. One fails and one succeeds. They both come in code,value pairs.

Both are attached to drop down menus. The Bldgs shows the result while the rooms fails with the above error.

The bldgs JSON retrieved from the server looks like this

[{
  "code": "Bldgs",
  "value": "A"
}, {
  "code": "Bldgs",
  "value": "J"
}, {
  "code": "Bldgs",
  "value": "I"
}, {
  "code": "Bldgs",
  "value": "H"
}, {
  "code": "Bldgs",
  "value": "G"
}, {
  "code": "Bldgs",
  "value": "F"
}, {
  "code": "Bldgs",
  "value": "E"
}, {
  "code": "Bldgs",
  "value": "D"
}, {
  "code": "Bldgs",
  "value": "C"
}, {
  "code": "Bldgs",
  "value": "B"
}, {
  "code": "Bldgs",
  "value": "K"
}]

Where as the Rooms look like this

[{
  "code": "Rooms",
  "value": "1"
}, {
  "code": "Rooms",
  "value": "7"
}, {
  "code": "Rooms",
  "value": "6"
}, {
  "code": "Rooms",
  "value": "4"
}, {
  "code": "Rooms",
  "value": "3"
}, {
  "code": "Rooms",
  "value": "2"
}, {
  "code": "Rooms",
  "value": "16"
}, {
  "code": "Rooms",
  "value": "15"
}, {
  "code": "Rooms",
  "value": "14"
}, {
  "code": "Rooms",
  "value": "13"
}, {
  "code": "Rooms",
  "value": "12"
}, {
  "code": "Rooms",
  "value": "11"
}, {
  "code": "Rooms",
  "value": "9"
}]

I tried

   var roomList,bldgList= [];
   dropdowns.listdrops({'code':'Rooms'})
    .$promise
    .then(
        function(data) {
            $log.info("Rooms:" + data.length);
             if (typeof data != "undefined") {
                for(var i = 0, len = data.length; i < len; i++) {
                    roomList[i] = data[i].value;    //ERROR HERE        
                };   
             }                      
        }
    );

   dropdowns.listdrops({'code':'Bldgs'})
    .$promise
    .then(
        function(data) {
            $log.info("Blgds:" + data.length);
            if (typeof data != "undefined") {
                for(var i = 0, len = data.length; i < len; i++) {
                    bldgList[i] = data[i].value;    //NO ERROR         
                };                          
            } else {
                $log.info('ERROR: no Drops');
            }
        }, function(error) {
            $log.info('No Drop downs- Server error');
        }   
    );

在此处输入图片说明

I even tried bypassing the server service call and doing this

   var test =               [{"code":"Rooms","value":"1"},ode":"Rooms","value":"7"},{"code":"Rooms","value":"6"},{"code":"Rooms","value":"4"},{"code":"Rooms","value":"3"},{"code":"Rooms","value":"2"},{"code":"Rooms","value":"16"},{"code":"Rooms","value":"15"},{"code":"Rooms","value":"14"},{"code":"Rooms","value":"13"},{"code":"Rooms","value":"12"},{"code":"Rooms","value":"11"},{"code":"Rooms","value":"9"}];`
   for(var i = 0, len = test.length; i < len; i++) {
        roomList[i] = test[i].value;    //ERROR        
   }; 

You need to initialise roomList. var roomList,bldgList= []; should be

var roomList = [],
    bldgList = [];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM