I am developing a web application in this case a front-end where I need to display Facebook events. I define an array called events. Inside each new event I have:
- id : id of the event.
- name : name of the event.
- description : description of the event.
- start_date : the start date of the event.
- end_date : the end date of the event.
- participants : array with participants in the event
- pictures : array of pictures of the event.
- places : array of places in the event.
var events = [];
var new_event = {
'id': event_id,
'name': event_name,
'description': event_description,
'start_date': event_start_date,
'end_date': event_end_date,
'participants' : [],
'pictures' : [],
'places' : []
};
then i create several participants, pictures and places.
var new_participant = {
'id': participant_id,
'name': participant_name,
'birthDate': participant_birth_date,
'pictureUrl': participant_photo_url
};
Every new participant has the variables on top.
and i make the following pushes:
events.push(new_event);
events[i].participants.push(new_participant);
i
goes from 0
to events.length - 1
.
but I get the following error in the browser:
Uncaught TypeError: Cannot read property 'participants' of undefined
What is the correct way of pushing arrays into javascript elements and how do I access them?
When I try to access:
events[i].participants[0].name
where i
is between 0 and events.length-1
, I get the same error for the name
property.
Uncaught TypeError: Cannot read property 'name' of undefined
Here is the full code:
$(document)
.ready(
function() {
var events = [];
$.ajax({
type : "GET",
url : "...",
dataType : "xml",
success : function(xml) {
$(xml).find('events').each(function() {
var event_id = $(this).find('id').text();
var event_name = $(this).find('name').text();
var event_description = $(this).find('description').text();
var event_start_date = $(this).find('startdate').text();
var event_end_date = $(this).find('enddate').text();
var new_event = {
'id': event_id,
'name': event_name,
'description': event_description,
'start_date': event_start_date,
'end_date': event_end_date,
'participants' : [],
'pictures' : [],
'places' : []
};
events.push(new_event);
});
for(var i=0; i < events.length; i++){
$.ajax({
type : "GET",
url : "...",
dataType : "xml",
success : function(xml) {
$(xml).find('user').each(function() {
var participant_id = $(this).find('id').text();
var participant_name = $(this).find('name').text();
var participant_birth_date = $(this).find('birthDate').text();
var participant_photo_url = $(this).find('pictureUrl').text();
if(participant_name !== ""){
var new_participant = {
'id': participant_id,
'name': participant_name,
'birthDate': participant_birth_date,
'pictureUrl': participant_photo_url
};
events[i].participants.push(new_participant);
}
});
},
error : function(xhr) {
alert(xhr.responseText);
}
});
$.ajax({
type : "GET",
url : "...",
dataType : "xml",
success : function(xml) {
$(xml).find('place').each(function() {
var place_id = $(this).find('id').text();
var place_name = $(this).find('name').text();
if(place_name !== ""){
var new_place = {
'id': place_id,
'name': place_name,
};
events[i].places.push(new_place);
alert("novo local: "+new_place.name);
}
});
},
error : function(xhr) {
alert(xhr.responseText);
}
});
$.ajax({
type : "GET",
url : "...",
dataType : "xml",
success : function(xml) {
$(xml).find('media').each(function() {
var media_id = $(this).find('id').text();
var media_url = $(this).find('url').text();
var media_content = $(this).find('content').text();
if(media_url !== ""){
var new_media = {
'id': media_id,
'url': media_url,
'content' : media_content
};
events[i].pictures.push(new_media);
alert("novo media: "+new_media.url);
}
});
},
error : function(xhr) {
alert(xhr.responseText);
}
});
}
var result_html = '';
var index = 0;
result_html += '<div class="f-page f-cover"><div class="cover-elements"><div class="f-cover-story"><span>Life Events</span>Photo Album</div></div><div class="f-cover-flip">< swipe</div></div>';
for(var i = events.length; i > 0 ; i -= 5 ){
result_html += '<div class="f-page">';
result_html += '<div class="f-title">';
result_html += '<a href="index.jsp">Back to bookshelf</a>';
result_html += '<h2 id="event_name">Your Life Events</h2>';
result_html += '<a href="#"></a>';
result_html += '</div>';
if(index < events.length){
$('.img-1').css({'background-image: ': 'url('+events[index].pictures[0].url+')'});
result_html += '<div class="box w-25 h-70" id="top_left_box"><div class="img-cont img-1"></div><h3>'
+ events[index].name + '<span>Published ' + 'From: ' + events[index].start_date + 'to: ' + events[index].end_date + '</span></h3><p>'
+ events[index].description + ' With: ' + events[index].participants[0].name + events[index].participants[0].pictureUrl + '</p></div>';
index++;
}
if(index < events.length){
$('.img-2').css({'background-image: ': 'url('+events[index].pictures[0].url+')', 'height: ': '60%'});
result_html += '<div class="box w-50 h-70 box-b-l box-b-r" id="top_center_box"><div class="img-cont img-2"></div><h3>'
+ events[index].name + '<span>Published ' + 'From: ' + events[index].start_date + 'to: ' + events[index].end_date + '</span></h3><p>'
+ events[index].description + ' With: ' + events[index].participants[0].name + events[index].participants[0].pictureUrl + '</p></div>';
index++;
}
if(index < events.length){
$(".img-3").css({'background-image: ': 'url('+events[index].pictures[0].url+')'});
result_html += '<div class="box w-25 h-70" id="top_right_box"><div class="img-cont img-3"></div><h3>'
+ events[index].name + '<span>Published ' + 'From: ' + events[index].start_date + 'to: ' + events[index].end_date + '</span></h3><p>'
+ events[index].description + ' With: ' + events[index].participants[0].name + events[index].participants[0].pictureUrl + '</p></div>';
index++;
}
if(index < events.length){
result_html += '<div class="box w-50 h-30 box-b-r title-top" id="bottom_left_box"><h3>'
+ events[index].name + '<span>Published ' + 'From: ' + events[index].start_date + 'to: ' + events[index].end_date + '</span></h3><p>'
+ events[index].description + ' With: ' + events[index].participants[0].name + events[index].participants[0].pictureUrl + '</p></div>';
index++;
}
if(index < events.length){
result_html += '<div class="box w-50 h-30 title-top" id="bottom_right_box"><h3>'
+ events[index].name + '<span>Published ' + 'From: ' + events[index].start_date + 'to: ' + events[index].end_date + '</span></h3><p>'
+ events[index].description + ' With: ' + events[index].participants[0].name + events[index].participants[0].pictureUrl + '</p></div>';
index++;
}
result_html += '</div>';
}
result_html += '<div class="f-page f-cover-back"><div id="codrops-ad-wrapper"><a href="index.jsp" >Back to bookshelf</a></div></div>';
$('#flip').html(result_html);
},
error : function(xhr) {
alert(xhr.responseText
+ " Error retrieving data from server");
}
});
});
After following suggestion here is what i got to:
$(document)
.ready(
function() {
var events = [];
$.ajax({
type : "GET",
url : "",
dataType : "xml",
success : function(xml) {
$(xml).find('events').each(function() {
var event_id = $(this).find('id').text();
var event_name = $(this).find('name').text();
var event_description = $(this).find('description').text();
var event_start_date = $(this).find('startdate').text();
var event_end_date = $(this).find('enddate').text();
var new_event = {
'id': event_id,
'name': event_name,
'description': event_description,
'start_date': event_start_date,
'end_date': event_end_date,
'participants' : [],
'pictures' : [],
'places' : []
};
events.push(new_event);
});
events.forEach(function(event){
$.ajax({
type : "GET",
url : "..."+ event.id + "...",
dataType : "xml",
success : function(xml) {
$(xml).find('user').each(function() {
var participant_id = $(this).find('id').text();
var participant_name = $(this).find('name').text();
var participant_birth_date = $(this).find('birthDate').text();
var participant_photo_url = $(this).find('pictureUrl').text();
if(participant_name !== ""){
var new_participant = {
'id': participant_id,
'name': participant_name,
'birthDate': participant_birth_date,
'pictureUrl': participant_photo_url
};
event.participants.push(new_participant);
}
});
},
error : function(xhr) {
alert(xhr.responseText);
}
});
$.ajax({
type : "GET",
url : "..."+ event.id + "...",
dataType : "xml",
success : function(xml) {
$(xml).find('place').each(function() {
var place_id = $(this).find('id').text();
var place_name = $(this).find('name').text();
if(place_name !== ""){
var new_place = {
'id': place_id,
'name': place_name,
};
event.places.push(new_place);
}
});
},
error : function(xhr) {
alert(xhr.responseText);
}
});
$.ajax({
type : "GET",
url : "..."+ event.id + "...",
dataType : "xml",
success : function(xml) {
$(xml).find('media').each(function() {
var media_id = $(this).find('id').text();
var media_url = $(this).find('url').text();
var media_content = $(this).find('content').text();
if(media_url !== ""){
var new_media = {
'id': media_id,
'url': media_url,
'content' : media_content
};
event.pictures.push(new_media);
}
});
},
error : function(xhr) {
alert(xhr.responseText);
}
});
alert(event.participants[0].name);
alert(event.pictures[0].url);
alert(event.places[0].name);
});
Still having the same error in name :
Uncaught TypeError: Cannot read property 'name' of undefined