Search code examples
javascriptjqueryeventbrite

Undefined Object Jquery/javascript


I'm trying to figure out the Eventbrite api and I've gotten something to work, but not what I want.

Below is my code. The first script works. The second one does not and yields this error: 102TypeError: undefined is not an object (evaluating 'data.name.text')

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
  var settings = {
    "async": true,
    "crossDomain": true,
    "url": "https://www.eventbriteapi.com/v3/events/17920884849/?token=NR4COIXFSOYKGDDG6MSP",
    "method": "GET",
    "headers": {}
  }

  $.ajax(settings).done(function (data) {
    console.log(data);
    var content = "<h2>" + data.name.text + "</h2>" + data.description.html;
    jQuery("#eventbrite").append(content);
  });
</script>
<div id="eventbrite"></div>

<h1>Another test</h1>

<script>
  var settings = {
    "async": true,
    "crossDomain": true,
    "url": "https://www.eventbriteapi.com/v3/organizers/12748782127/events/?token=ZMDFKJWCHAMX5WFVPBAJ",
    "method": "GET",
    "headers": {}
  }

  jQuery.ajax(settings).done(function (data) {
    console.log(data);
    var content = "<h2>" + data.name.text + "</h2>" + data.description.html;
    jQuery("#eventbrite2").append(content);
  });
</script>
<div id="eventbrite2"></div>

Thanks.


Solution

  • Look at the JSON that's returned.

    The first call looks like this, so data.name works fine:

       {
        "name": {
            "text": "An Aggressive Approach to Concise Writing, with Joe Welinske", 
            "html": "An Aggressive Approach to Concise Writing, with Joe Welinske"
        }, 
    

    The second call looks like this, so you'll need data.events[0].name.text:

    {
    "pagination": {
        "object_count": 1, 
        "page_number": 1, 
        "page_size": 50, 
        "page_count": 1
    }, 
    "events": [
        {
            "name": {
                "text": "Test Event 1", 
                "html": "Test Event 1"