I am developing an application that is basically a front-end it is mostly jquery and html.
It is basically a front-end to simplify the facebook events. Here is my code:
in this page loadEvents.js i communicate with webservices and store events inside an array of events:
Inside the first successfull request to the webservices i create the new event i am about to add.
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' : []
};
for each event i read from the returned xml i store it the following way and it works fine.
events.push(new_event);
Although i then make new requests for participants, pictures and places where those events took place. And here is where the code starts not to working. Let elaborate.
for each event and each participant returned by the webservice in xml format i
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);
}
I use the same methodology for places and pictures as in here. although when i then try to use the variables like this:
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++;
}
.
.
.
.
.
.
}
$('#flip').html(result_html);
}
It tells me that variable url, name and pictureUrl cannot read property of undefined. I think it might be the way i am declaring the new arrays inside the new_event variable or the way i am adding new elements into that array.
I am having a problem in here:
events[i].participants.push(new_participant);
it says
19 Uncaught TypeError: Cannot read property 'participants' of undefined
why is this happening? i set everything the values are getting fetched from the webservice perfectly. i think the problem is in the way i create the participants inside the new_event var and/or do this push.
So you can see the full code i made if you want here it goes:
$(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");
}
});
});