2016-04-07 25 views
0

現在、私は検索するキーワードからすべてのツイートを読み込もうとしています。実際には10の結果がありますが、私は3つの結果しか表示されず、コンソールウィンドウにエラーメッセージが表示されています。Uncaught TypeError:ヌルのプロパティ 'bounding_box'を読み取ることができません。

これはエラーメッセージです:

Uncaught TypeError: Cannot read property 'bounding_box' of null 

これは私のjsのコードです:

(function(){ 
    var location = new Array(); 
    var query = '%23CM0655'; 
    var url = "search.php"; 
    $.post(url, {query:query}, function(tweets){ 
     console.log(tweets); 
     $("#tweetResult tbody").html(""); 
     var geoEnabled = false; 
     var placeName = ""; 
     var countryName = ""; 

     for(var i=0; i<tweets.statuses.length; i++){ 
      var row = $("<tr></tr>"); 
      var img = $("<td><div class='div_pic'><img src='" + tweets.statuses[i].user.profile_image_url + "' class='tweetpic'/>"+ 
         "<label class='user_name' hidden>"+tweets.statuses[i].user.screen_name+"</label>"+ 
         "<label class='divlatitude' hidden>"+tweets.statuses[i].place.bounding_box.coordinates[0][0][0]+"</label>"+ 
         "<label class='divlongtitude' hidden>"+tweets.statuses[i].place.bounding_box.coordinates[0][0][1]+"</label>"+ 
         "</div></td>"); 
      //var img = $("<td><div class='div_pic'><img src='" + tweets.statuses[i].user.profile_image_url + "' class='tweetpic'/></div></td>"); 
      row.append(img); 
       // row.append($("<td></td>").html(tweets.statuses[i].user.screen_name)); 

      row.append($("<td></td>").html(tweets.statuses[i].user.screen_name)); 
      row.append($("<td></td>").html(tweets.statuses[i].text + "<br/>")); 
      geoEnabled = tweets.statuses[i].user.geo_enabled; 
      if(geoEnabled){ 
       placeName = tweets.statuses[i].place.name; 
       countryName = tweets.statuses[i].place.country; 
       if(placeName != null){ 
        row.append($("<td></td>").html(placeName + "," + countryName + "<br/>")); 
       } 
       row.append($("<td></td>").html("Location: " + tweets.statuses[i].place.bounding_box.coordinates[0][0][0] + ", " + 
        tweets.statuses[i].place.bounding_box.coordinates[0][0][1] + "<br/>")); 
       row.append($("<td></td>").html(tweets.statuses[i].created_at)); 
      } 
      $("#tweetResult tbody").append(row) 

     } 
    }); 

    setTimeout(arguments.callee, 60000); 
})(); 

これは、エラーを示している行です。

"<label class='divlatitude' hidden>"+tweets.statuses[i].place.bounding_box.coordinates[0][0][0]+"</label>"+ 

は、任意のはありますこのエラーを解決する方法。特定のつぶやきの場所が有効でない場合でも、すべてのつぶやきを表示したい誰かが私にこれを助けることができますか?ありがとうございました。

答えて

0

変更tweets.statuses[i].place.bounding_box.coordinates[0][0][0]あなたが複数の場所でこのスニペットを使用しているので、私は変数にそれを保存するためにあなたを提案し、それを使用することになり

tweets.statuses[i].place ? tweets.statuses[i].place.bounding_box.coordinates[0][0][0] : "No location data" 

に次。

通常、取得するデータオブジェクトにはnullまたはundefinedが含まれていますが、常に安全に再生する方が良いです。あなたは、あなたはきっとcoordinates[0][0]bounding_box持つことになるしている場合、と仮定すると、

var location = (tweets.statuses[i].place && tweets.statuses[i].place.bounding_box && tweets.statuses[i].place.bounding_box.coordinates[0][0][0]) || "No location"; 

:上記のコードは次のようにすることができます。

0

私はそれ

if (tweets.statuses[i].user.geo_enabled && tweets.statuses[i].place != null) { 

       var img = $("<td><div class='div_pic'><img src='" + tweets.statuses[i].user.profile_image_url + "' class='tweetpic'/>"+ 
          "<label class='user_name' hidden>"+tweets.statuses[i].user.screen_name+"</label>"+ 
          "<label class='divlatitude' hidden>"+tweets.statuses[i].place.bounding_box.coordinates[0][0][0]+"</label>"+ 
          "<label class='divlongtitude' hidden>"+tweets.statuses[i].place.bounding_box.coordinates[0][0][1]+"</label>"+ 
          "</div></td>"); 
      }else{ 

       var img = $("<td><div class='div_pic'><img src='" + tweets.statuses[i].user.profile_image_url + "' class='tweetpic'/>"+ 
        "<label class='user_name' hidden>"+tweets.statuses[i].user.screen_name+"</label>"+ 
        "<label class='divlatitude' hidden>null</label>"+ 
        "<label class='divlongtitude' hidden>"+tweets.statuses[i].user.location+"</label>"+ 
        "</div></td>"); 
      } 
周りのif-else文を追加することによって、それを解決しました
関連する問題