Utilizing the Ticketmaster API has provided me with a sample dataset from their platform,
Data 1 - Including information on "presales."
"sales": {
"public": {
"startDateTime": "2019-11-22T17:00:00Z",
"startTBD": false,
"startTBA": false,
"endDateTime": "2021-09-25T23:00:00Z"
},
"presales": [
{
"startDateTime": "2019-11-18T16:37:00Z",
"endDateTime": "2019-11-22T04:00:00Z",
"name": "Verified Fan Presale"
},
// Other presale data entries...
]
}
Data 2 - Without any records of "presales."
"sales": {
"public"': {
"startDateTime": "2020-03-06T15:00:00Z",
"startTBD": false,
"startTBA": false,
"endDateTime": "2021-11-01T00:00:00Z"
}
}
I have multiple datasets, some containing presale details and others not. I'm iterating through all available data retrieved from the API.
Below is my source code, where I encounter an issue with json.has("presales")
, causing an error.
// JavaScript function to retrieve event data
function showSearch(){
var kword = document.getElementById("searchTxt").value;
var inputCountry = document.getElementById("inputCountry").value;
var startdateid = new Date(document.getElementById("startdateid").value);
var enddateid = new Date(document.getElementById("enddateid").value);
// Check if dates are valid
if ( isNaN( startdateid.getTime() ) ) {
startdateid = "invalid";
}
if ( isNaN( enddateid.getTime() ) ) {
enddateid = "invalid";
}
// AJAX call to Ticketmaster API
$.ajax({
type:"GET",
url:"https://app.ticketmaster.com/discovery/v2/events.json?keyword=" + kword + "&countryCode="+ inputCountry +"&size=200&apikey=Q8G0vn5ycUgMPGsnCGqLe1ZCP6GyY1uR",
async:true,
dataType: "json",
success: function(json) {
console.log(json);
var e = document.getElementById("eventsfound");
e.innerHTML = json.page.totalElements + " events found.";
showEvents(json,startdateid,enddateid); // Display events in table
},
error: function(xhr, status, err) {
}
});
}
// Function to display events in DataTable
function showEvents(json,startdateid,enddateid) {
var table = $('#example2').DataTable();
var presaleStartCheck;
var presaleEndCheck;
var presaleNameCheck;
table.clear();
for(var i=0; i<json.page.size; i++) {
var eDate = new Date(json._embedded.events[i].dates.start.localDate);
var counter = i + 1;
if((startdateid=="invalid") && (enddateid=="invalid")){
if(json.sales.presales){ // Issue arises here, checking if presales exist
presaleStartCheck = json._embedded.events[i].sales.presales[i].startDateTime;
presaleEndCheck = json._embedded.events[i].sales.presales[i].endDateTime;
presaleNameCheck = json._embedded.events[i].sales.presales[i].name;
} else{
presaleStartCheck = "Not applicable";
presaleEndCheck = "Not applicable";
presaleNameCheck = "Not applicable";
}
// Populate table with event details
table.row.add( [
counter,
json._embedded.events[i].name,
json._embedded.events[i].dates.start.localDate,
json._embedded.events[i].dates.start.localTime,
json._embedded.events[i].url,
presaleStartCheck,
presaleEndCheck,
presaleNameCheck,
] ).draw();
}
}
}
showSearch(); // Invoke search function
In each iteration, I aim to handle scenarios where certain loops detect the presence of presale information. If presales exist, specific data should be utilized; otherwise, default values should be applied as indicated in the else statement.
Although the line
e.innerHTML = json.page.totalElements + " events found.";
functions correctly in displaying the count of retrieved events, I face challenges in showing data in the table due to the Uncaught TypeError: json.has is not a function
error related to json.has("presales")
.
Any guidance or support would be highly appreciated. Thank you sincerely!