2010-11-22 1 views
0

null値が返されない場合、このコードは有効です。しかし、テスト中に、値がnullの場合はスローしてエラーが発生し、残りのスクリプトが中断されることに気付きました。Facebook APIがnullを返す、JavaScriptをスキップする方法

nullの場合は値をスキップして、それ以外の値に設定するにはどうすればよいですか。

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); 
    } 

         }); 



        }); 

       }); 

ありがとうございます。 rows[0].fooがnullの場合

答えて

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

""(空の文字列)を返します。

+0

私はあなたの提案を試みました。それでも同じ結果が得られます。 rows [0] .current_location is null [このエラーで中断する] current_city = rows [0] .current_location.city || ""; – kkampen

+0

current_locationがnullでない場合は、current_location.cityにのみアクセスします。 – Yuliy

0

このように修正しました。

if(rows[0].current_location != null){ 
    current_city = rows[0].current_location.city || ""; 
    current_country = rows[0].current_location.country || ""; 
} 
関連する問題