admin管理员组

文章数量:1287201

I am trying to generate 5 random images (first row is from flicker and worked ok). On the second row it's from google, but it's only returning 4 for some reason and returning an error on the console that says:

Uncaught TypeError: Cannot read property 'url' of undefined

This is my HTML

 <div class="weleScreen">
  <form id="players">
    <p>Player 1</p>
    <input id="player1Name" placeholder="Enter player 1's name">

    <div class='player1Info clearfix'>
      <label>
        <img src="">
        <input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
      </label>
    </div>

    <p>Player 2</p>
    <input id="player2Name" placeholder="Enter player 2's name">

    <div class='player2Info clearfix'>
      <label>
        <img src="">
        <input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
      </label>
    </div>

    <input value="Start the race!" type="submit">
    </form>
</div>

Javascript

function buildFlickrUrl(p) {
    var url = "https://farm";
    url += p.farm;
    url += ".staticflickr/";
    url += p.server;
    url += "/";
    url += p.id;
    url += "_";
    url += p.secret;
    url += ".jpg";

    return url;
}

$(document).ready(function() {

    var flickrUrl = "/?    
    method=flickr.photos.search&format=json&api_key=
    4ef070a1a5e8d5fd19faf868213c8bd0&nojsoncallback=1&text=dog

    $.get(flickrUrl, function(response) { 
        for(var i = 0; i < 5; i++) {
            var photoUrl = buildFlickrUrl(response.photos.photo[i]);
            $(".player1Info label img").eq(i).attr('src', photoUrl);
            $(".player1 img").eq(i).attr('src' , photoUrl);
            console.log(photoUrl);
        }
    });

var input="cute kitten";

$.getJSON("=?", {
    q: input,
    v: '1.0'
}, 

Thanks for all your help!

I am trying to generate 5 random images (first row is from flicker and worked ok). On the second row it's from google, but it's only returning 4 for some reason and returning an error on the console that says:

Uncaught TypeError: Cannot read property 'url' of undefined

This is my HTML

 <div class="weleScreen">
  <form id="players">
    <p>Player 1</p>
    <input id="player1Name" placeholder="Enter player 1's name">

    <div class='player1Info clearfix'>
      <label>
        <img src="">
        <input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
      </label>
    </div>

    <p>Player 2</p>
    <input id="player2Name" placeholder="Enter player 2's name">

    <div class='player2Info clearfix'>
      <label>
        <img src="">
        <input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
      </label>

      <label>
        <img src="">
        <input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
      </label>
    </div>

    <input value="Start the race!" type="submit">
    </form>
</div>

Javascript

function buildFlickrUrl(p) {
    var url = "https://farm";
    url += p.farm;
    url += ".staticflickr./";
    url += p.server;
    url += "/";
    url += p.id;
    url += "_";
    url += p.secret;
    url += ".jpg";

    return url;
}

$(document).ready(function() {

    var flickrUrl = "https://www.flickr./services/rest/?    
    method=flickr.photos.search&format=json&api_key=
    4ef070a1a5e8d5fd19faf868213c8bd0&nojsoncallback=1&text=dog

    $.get(flickrUrl, function(response) { 
        for(var i = 0; i < 5; i++) {
            var photoUrl = buildFlickrUrl(response.photos.photo[i]);
            $(".player1Info label img").eq(i).attr('src', photoUrl);
            $(".player1 img").eq(i).attr('src' , photoUrl);
            console.log(photoUrl);
        }
    });

var input="cute kitten";

$.getJSON("https://ajax.googleapis./ajax/services/search/images?callback=?", {
    q: input,
    v: '1.0'
}, 

Thanks for all your help!

Share Improve this question edited Nov 14, 2014 at 20:12 Ken Ryan asked Nov 14, 2014 at 19:51 Ken RyanKen Ryan 5491 gold badge12 silver badges35 bronze badges 5
  • clearly data.responseData.results[i] doesn't have a url property, so you have to console.log data and see what you've really got. – adeneo Commented Nov 14, 2014 at 19:53
  • Looking in the network tab for the google request, the data retrieved only brings in 4 images, there is no 5th image – Patrick Evans Commented Nov 14, 2014 at 19:55
  • error means data.responseData.results[i] is undefined – A. Wolff Commented Nov 14, 2014 at 19:55
  • @PatrickEvans does for(var i = 0; i < 5; i++) not return 5 images? – Ken Ryan Commented Nov 14, 2014 at 20:01
  • Your for loop is setup to go from 0 to 4 (i<5), 0,1,2,3,4 <- how many is there: 5, so your loop executes that many times. – Patrick Evans Commented Nov 14, 2014 at 20:04
Add a ment  | 

2 Answers 2

Reset to default 3

Set a breakpoint in the console and look at the results being returned.

> data.responseData.results
[Object, Object, Object, Object]

You will see the results has a length of 4 and you are looping to read index of 4.

Change

for(var i = 0; i < 5; i++) {

to

for(var i = 0; i < data.responseData.results.length; i++) {

Your response data is ok, the problem is in your for loop.

The object data.responseData.results contains 4 elements and your looping since i < 5. Just change it to data.responseData.results.length.

 for(var i = 0; i < data.responseData.results.length ; i++) {
    var googleImageUrl = data.responseData.results[i].url;
    $(".player2Info label img").eq(i).attr('src', data.responseData.results[i].url);
    console.log(googleImageUrl);
  }

本文标签: javascriptUncaught TypeError Cannot read property 39url39 of undefinedGoogle Image APIStack Overflow