I'm working on a class project where I'm pulling data from ticketmaster API. I can generate a working api query by hardcoding the variables, but I'd like it to use a specific variable unless the user has inputted a date. For example, a user could search for events after 06-01-2019 and it would append the "dateSearch" variable, otherwise I'd like it to append the "today" variable instead.

I've gotten this to work with the keyword search, but it's not working with the dateSearch.

This is the search query. The "dateSearch" is what I'm trying to append to "query" else append "today".

function apiEvents(eventSearch) {
    console.log(eventSearch)
    const apiKey = "apikey=[APIKEY]&"
    const apiTM = "https://app.ticketmaster.com/discovery/v2/"
    let keyword = "keyword=" + $("#keyword-search").val() + "&";
    let city = "city=" + $("#city-search").val() + "&";
    let size = "size=6";
    var date = $("#date-search").val()
    var dateSearch = "localStartDateTime=" + moment(date).format("YYYY-MM-DDTHH:mm:ss") + ",*&sort=date,asc&"
    let query = apiTM + "events.json?" + apiKey;
    if ($("#city-search").val().length > 0) {
      query += city;

      if ($("#keyword-search").val().length > 0)
        query += keyword;

      if ($("#date-search").val() > 0)
      query += dateSearch;
      console.log(dateSearch)

    } else {
      // alert("Please tell us your search conditions.");
    }
    console.log(query);
    api(query);

  }

Both the below work seperately, but I can't get the code to dynamically add in one or the other based on the search field being empty.

let query = apiTM + "events.json?" + dateSearch + apiKey;
let query = apiTM + "events.json?" + today + apiKey;

0 Answers