benhowdle89
benhowdle89

Reputation: 37464

jQuery check if returned JSON is NULL

I have this returned JSON:

results: Array[15]
0: Object
created_at: "Wed, 10 Aug 2011 22:45:36 +0000"
from_user: "CriisBellaFlor"
from_user_id: 360990380
from_user_id_str: "360990380"
geo: null

I'm using this code to loop around it:

var twitterUrl='http://search.twitter.com/search.json?callback=?&q=';
                query='cinema';
                $.getJSON(twitterUrl+query,function(json){
                    console.log(json);
                    $.each(json.results,function(i,tweet){
                        if(tweet.iso_language_code == 'en'){
                            if(tweet.geo === 0){
                                $(cinemas).append('<img src="'+tweet.profile_image_url+'" width="48" height="48" />'+tweet.text+'<br />');
                            } else {
                                $(cinemas).append('<img src="'+tweet.profile_image_url+'" width="48" height="48" />'+tweet.text+'Coordinates:' + tweet.geo.coordinates[0] + ' + ' + tweet.geo.coordinates[1] + '<br />');
                            }

                        }
                    });
                });

So what i'm trying to determine is, if "geo" is null, dont try and display the coordinates, if there is something there, display them.

But i get this error:

Uncaught TypeError: Cannot read property 'coordinates' of null

Upvotes: 3

Views: 16528

Answers (1)

Jacek Kaniuk
Jacek Kaniuk

Reputation: 5229

try that

if(tweet.geo === null){

BTW, === means that comparison checks also type of variable

Upvotes: 5

Related Questions