2010-11-22 1 views
0

Dieser Code funktioniert großartig, wenn keine Nullwerte zurückgegeben werden. Aber ich bemerkte während des Tests, dass, wenn irgendein Wert Null ist, es einen Fehler wirft und den Rest des Skripts unterbricht.Facebook API gibt null zurück, wie Sie es in Javascript überspringen

Wie kann ich den Wert überspringen, wenn Null, oder setzen Sie es auf etwas anderes.

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

         }); 



        }); 

       }); 

danke im voraus.

Antwort

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

wird "" (eine leere Zeichenkette) zurück, wenn rows[0].foo null ist.

+0

Ich habe gerade Ihren Vorschlag versucht. Immer noch die gleichen Ergebnisse. rows [0] .current_location ist null [Pause bei diesem Fehler] current_city = rows [0] .current_location.city || ""; – kkampen

+0

dann nur current_location.city zugreifen, wenn current_location nicht null ist. – Yuliy

0

Ich reparierte es so.

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