Facebook API 返回 null,如何在 Javascript 中跳过它

发布于 2024-10-04 07:07:40 字数 1645 浏览 5 评论 0原文

如果没有返回空值,则此代码可以很好地工作。但我在测试过程中注意到,如果任何值为空,则会抛出错误并破坏脚本的其余部分。

如果为空,我如何跳过该值,或将其设置为其他值。

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

                        });



                     });

                });

提前致谢。

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 + "¤t_city=" + current_city + "¤t_country=" + current_country,

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

                        });



                     });

                });

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

塔塔猫 2024-10-11 07:07:40
var foo = rows[0].foo || "";

如果 rows[0].foo 为 null,将返回 "" (空字符串)。

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

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

无法言说的痛 2024-10-11 07:07:40

我是这样修好的。

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

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 || "";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文