Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Erik L
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik L
Full Stack JavaScript Techdegree Graduate 19,470 Points

get JSON request not working??

hello I have completed the following video https:// .com/library/ajax-basics/ajax-and-apis/displaying-the-photos, but the getJSON request is giving me this error on developer tools: Uncaught TypeError: Cannot read property 'length' of undefined, this is my code:

html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>AJAX Flickr Photo Feeder</title>
  <link href='http://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="css/main.css">
  <script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
  <script src="js/flickr.js"></script>
</head>
<body>
  <div class="grid-container centered">
    <div class="grid-100">
      <div class="contained">
        <div class="grid-100">
          <div class="heading">
            <h1>Flickr Photo Feeder</h1>
            <p>Choose which kind of animal you'd like to return photos of...</p>
          </div>
          <ul class="filter-select">
            <li><button>Dog</button></li>
            <li><button>Cat</button></li>
            <li><button>Moose</button></li>
          </ul>
        </div>

        <div id="photos">

        </div>
      </div>
    </div>
  </div>
</body>
</html>

and JS:

\$(document).ready(function () {
    //add event handler to all buttons
  $('button').click(function () {
      //remove selected class from all buttons when one button is clicked
      $('button').removeClass('selected');
      //when a button is clicked, add a class called "selected"
    $(this).addClass('selected');
    /*use Jquery's getJSON method to get data from flickr when a visitor pushes a button,
    first parameter is URL to resource, second is data we want to send along with URL,
    the third argument is a callback function
    */

    //put flickr api url inside a variable
   var flickerAPI = "https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";

   //store animals words inside a variable, (this) refers to the buttons that the visitor clicks
   var animal = $(this).text

   /*create data to be sent by making a js object, 
   by using tags flickr will search for photos associated with animal*/
   var flickrOptions = {
    tags: animal,
    format: "json" 
  };
  function displayPhotos(data) {
    //create a variable to store HTML that will load into <ul class="filter-select">
    var photoHTML = '<ul>';
    $.each(data.items, function (i, photo) {
      //add li element
      photoHTML += '<li class=grid-25 tablet-grid-50>';
      //add a href
      photoHTML += '<a href="'+ photo.link + '" class="image">';
      //add img src
      photoHTML += '<img src="' + photo.media.m + '"></a>,</li>';
    });
    //close ul tag
    photoHTML += '</ul>';
    //add html to page
    $('#photos').html(photoHTML);
  }

    $.getJSON(flickerAPI, flickrOptions, displayPhotos);
  });  
}) //end ready
luther wardle
seal-mask
.a{fill-rule:evenodd;}techdegree
luther wardle
Full Stack JavaScript Techdegree Student 18,029 Points

its possible that your JSON object:

var flickrOptions = { tags: animal, format: "json" };

is not formatted correctly, perhaps you can try

var flickrOptions = { tags: "animal", format: "json" };

Not sure if it will work, just a thought

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Try fixing the following line first:

var animal = $(this).text

// Should be something like below if you want the button text
var animal = $(this).text();