2017-04-20 350 views
0

我有一個問題來訪問一個javascript對象的屬性。 我有一個ajax調用,我創建一個對象,並在完整的一部分,我想使用創建的對象,但我無法訪問屬性,當我控制檯登錄ojbect它在那裏,但不能訪問屬性 這是我的代碼:無法訪問javascript對象的屬性

這裏面的Ajax調用 治療我有誰在AJAX完成創建一個對象

var repartion = getEventsByCity(villes,date); 

我即使.done嘗試的功能:

console.log(repartion) 

將其打印

Object {} 
75001: Array(4) 
      0: Object 
      1: Object 
      2: Object 
      3: Object 
      length: 4 
      __proto__: Array(0) 
      __proto__: Object 

所以對象是定義,但如果我嘗試

console.log(repartion['75001']); 

後打印未定義 如果我嘗試

$.each(repartion, function(index,value){ 
    console.log(index + " " + value); 
}); 

其打印什麼... 我不不明白,有人可以幫助我找到我做錯了什麼,我不明白。

謝謝。

更新: 我表現出更多的我的代碼

,這是我的Ajax調用

function callApi(data){ 
    $.ajax({ 
     url: 'events_finder.js', 
     data: data, 
     success: function(arg){ console.log('success ');}, 
     complete: function(arg){ 
      console.log('complete '); 
      // this one working and print the object 
      console.log(repartion); 
      // not showing anything 
      $.each(repartion, function(index,value){ 
       console.dir(index + " " + value); 
      }); 
      // showing undefined 
      console.log(repartion['75001']); 
     }, 
     error: function(xhr,status,erreur){ console.log(xhr.status+" "+status+" "+ erreur);}, 
     }).done(function(){ 
     console.log("DONE"); 
     // this one working and print the object 
      console.log(repartion); 
      // not showing anything 
      $.each(repartion, function(index,value){ 
       console.dir(index + " " + value); 
      }); 
      // showing undefined 
      console.log(repartion['75001']); 
     }) 
    } 

這events_finder.js

var villes = data['villes']; 
var date = data['date']; 
// i put only one city for the debug 
villes = ['75001'] 

function getEventsByCity (villes,date){ 
    var repartion_events = {}; 
    for(i = 0 ; i < villes.length ; i++){ 
    var oArgs = { 
     app_key: "fzgree4546Rx" , 
     where: "France, "+villes[i], 
     date: date, 
     sort_order: "popularity", 
     page_size: "400", 
    }; 
    //console.log(oArgs); 

    EVDB.API.call("/events/search", oArgs, function(oData) { 
    //console.log(oData); 
     // console.log(typeof oData['events']['event']); 
     if(oData['events'] != null){ 
     if (oData['events']['event'] != null){ 
      var kikou = oData['events']['event']; 
      //console.log(kikou); 
      var eventsByCity = new Array; 
      $.each(kikou, function(index, value) { 
      var events = { 
       "description": kikou[index]['description'], 
       "image": kikou[index]['image'], 
       "latitude": kikou[index]['latitude'], 
       "longitude": kikou[index]['longitude'], 
       "postal_code": kikou[index]['postal_code'], 
       "start_time": kikou[index]['start_time'], 
       "stop_time": kikou[index]['stop_time'], 
       "title": kikou[index]['title'], 
       "url": kikou[index]['url'], 
       "venue_address": kikou[index]['venue_address'], 
       "venue_name": kikou[index]['venue_name'], 
       "venue_url": kikou[index]['venue_url'] 
      }; 
      // rajouter l'objet dans le tableau de la ville 
      eventsByCity.push(events); 
      ville = kikou[index]['postal_code']; 
      }); 
      // console.log(eventsByCity); 
      // console.log(ville); 

      repartion_events[ville] = eventsByCity;   

     } 
     } 
    }); 
    } 
return repartion_events; 
} 
var repartion = getEventsByCity(villes,date); 
// print the object this working 
console.log(repartion repartion['75001']); 
// this one show undefined 
console.log(repartion['75001']); 
+0

嘗試'的console.log(repartion [75001]);' – Iceman

+0

你嘗試這樣'的console.log(repartion [75001])的整數鍵;'? – dloeda

+1

我猜這有[與Ajax調用有關](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call)。你必須展示[mcve]。 – JJJ

回答

-1

檢查repartion存在,而你打印repartion['75001']

console.log(repartion && repartion['75001']);

隨着代碼,如果是repartion存在,它將打印repartion [ '75001']。你可以嘗試一下。

而且你可以離開我的評論,

+0

我試過仍未定義 –