开发者

Facebook API returns null, how to skip it in Javascript

开发者 https://www.devze.com 2023-01-26 21:47 出处:网络
This code work great if there are no null values returned.But I noticed during testing that if any value is null then it throws and error and breaks the rest of the script.

This code work great if there are no null values returned. But I noticed during testing that if any value is null then it throws and error and breaks the rest of the script.

How can i skip the value if null, or set it to something else.

function fqlQuery(){
                FB.api('/me', function(response) {
                     var query = FB.Data.query('SELECT uid, first_name, last_name, email, sex, pic_square, timezone, birthday_date, current_location FROM user WHERE uid = me()', response.id);
                     query.wait(function(rows) {
                        fb_uid = rows[0].uid;
                        first_name = rows[0].first_name;
                        last_name = rows[0].last_name;
                        sex = rows[0].sex;
                        email = rows[0].email;
                        fb_pic_square = rows[0].pic_square;
                        timezone = rows[0].timezone;
                        birthday_date = rows[0].birthday_date;
                     开发者_开发技巧   current_city = rows[0].current_location.city;
                        current_country = rows[0].current_location.country;
                        $.ajax({
                            type: "POST",
                            url: "postdb.php",
                            data: "first_name=" + first_name + "&last_name=" + last_name + "&email=" + email + "&fb_uid=" + fb_uid + "&sex=" + sex + "&fb_pic_square=" + fb_pic_square + "&timezone=" + timezone + "&birthday_date=" + birthday_date + "&current_city=" + current_city + "&current_country=" + current_country,

   success: function(msg){
     alert( "Data Saved: " + msg );
   }

                        });



                     });

                });

thanks in advance.


var foo = rows[0].foo || "";

will return "" (an empty string) if rows[0].foo is null.


I fixed it like this.

if(rows[0].current_location != null){
  current_city = rows[0].current_location.city || "";
  current_country = rows[0].current_location.country || "";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消