MrJoshFisher
MrJoshFisher

Reputation: 1123

Why can't I get the rating from this object?

I'm using http://netflixroulette.net/api/ to pull info based on what a type in the text box, but im having trouble getting the value of the objects parameters.

$('.moviebutton').on('click', function(e){
    e.preventDefault();

    var title = $('#moviename').val();
    var movieresults = $('#movieresults');
    var requestURL = '//netflixroulette.net/api/api.php';

    $.getJSON(requestURL, {
        'title' : title,
    }, function(data) {
        console.log(data);
        if(data){
            myMovie = '<table>';
            myMovie += '<tr><th>Rating</th><th>Title</th><th>Poster Link</th></tr>';
            $.each(data, function(i, rep){
                myMovie += '<tr>';
                myMovie += '<td>';
                    myMovie += rep.rating;
                myMovie += '</td>';
                myMovie += '<td>';
                    myMovie += rep.show_title;
                myMovie += '</td>';
                myMovie += '<td>';
                    myMovie += rep.poster;
                myMovie += '</td>';
                myMovie += '</tr>';
            });
            myMovie += '</table>';
        } else {
            myMovie = '<p>Sorry no address where found</p>';
        }
        movieresults.html(myMovie);
    });
});

the return for this is

Object {unit: 883, show_id: 70299043, show_title: "Attack on Titan", release_year: "2013", rating: "4.6"…}
    category:"Anime"
    director:""
    mediatype:1
    poster:"http://netflixroulette.net/api/posters/70299043.jpg"
    rating:"4.6"
    release_year:"2013"
    runtime:"24 min"
    show_cast:"Yuki Kaji, Yui Ishikawa, Marina Inoue, Daisuke Ono, Hiro Shimono, Hiroshi Kamiya, Keiji Fujiwara, Kishô Taniyama, Romi Park, Ryota Ohsaka"
    show_id:70299043
    show_title:"Attack on Titan"
    summary:"For over a century, people have been living behind barricades to block out the giant Titans that threaten to destroy the human race. When a Titan destroys his hometown, young Eren Yeager becomes determined to fight back."
    unit:883
    __proto__:Object

But when I try rep.rating or rep.show_title I get undefined, am I missing a Parameter or instead of rep.w/e would I use object.rating?

Upvotes: 0

Views: 40

Answers (1)

palaѕн
palaѕн

Reputation: 73956

Since the return value here is actually an object you can just use data.rating instead of using loop and calling rep.rating like:

$.getJSON(requestURL, {
  'title': title,
}, function(data) {
  console.log(data);
  if (data) {
    myMovie = '<table>';
    myMovie += '<tr><th>Rating</th><th>Title</th><th>Poster Link</th></tr>';
    myMovie += '<tr>';
    myMovie += '<td>';
    myMovie += data.rating;
    myMovie += '</td>';
    myMovie += '<td>';
    myMovie += data.show_title;
    myMovie += '</td>';
    myMovie += '<td>';
    myMovie += data.poster;
    myMovie += '</td>';
    myMovie += '</tr>';
    myMovie += '</table>';
  } else {
    myMovie = '<p>Sorry no address where found</p>';
  }
  movieresults.html(myMovie);
});

Simple Demo:

var movieresults = $('#movieresults');
var requestURL = 'http://netflixroulette.net/api/api.php?title=Attack%20on%20titan';

$.getJSON(requestURL, function(data) {
  console.log(data);
  if (data) {
    myMovie = '<table>';
    myMovie += '<tr><th>Rating</th><th>Title</th><th>Poster Link</th></tr>';
    myMovie += '<tr>';
    myMovie += '<td>';
    myMovie += data.rating;
    myMovie += '</td>';
    myMovie += '<td>';
    myMovie += data.show_title;
    myMovie += '</td>';
    myMovie += '<td>';
    myMovie += data.poster;
    myMovie += '</td>';
    myMovie += '</tr>';
    myMovie += '</table>';
  } else {
    myMovie = '<p>Sorry no address where found</p>';
  }
  movieresults.html(myMovie);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<p id="movieresults"></p>

Upvotes: 1

Related Questions