2016-02-12 167 views
0

我有兩個JSON對象,一個持有建築物,另一個持有房間。一個失敗,一個成功。它們都以代碼,值對的形式出現。JSON對象TypeError:無法設置未定義的屬性'0'

兩者都附在下拉菜單中。建築物顯示結果,而房間因上述錯誤而失敗。

從服務器檢索到的bldgs JSON看起來像這樣

[{ 
    "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" 
}] 

凡作爲房間看起來像這樣

[{ 
    "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" 
}] 

我試圖

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'); 
     } 
    ); 

enter image description here

我甚至嘗試繞過服務器服務電話和這樣

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   
    }; 
+0

你永遠不會初始化'roomList' – Hacketo

回答

0

您需要初始化roomList。 var roomList,bldgList= [];應該是

var roomList = [], 
    bldgList = []; 
+0

謝謝!感覺正式轉儲! LOOL .... – devsahem

相關問題