2

我将我的项目与 Facebook 授权集成https://developers.facebook.com/docs/facebook-login/web

Rollbar 显示下一个错误

TypeError:this.onready 不是函数。(在 'this.onready(a)' 中,'this.onready' 未定义)

https://connect.facebook.net/en_US/sdk.js来自facebook IOS 应用程序(版本 172、171、170)内部浏览器的文件。

请告诉我问题是什么以及如何解决?

$.ajaxSetup({cache: true});
window.fbAsyncInit = function() {
    if(typeof FB != 'undefined'){
        FB.init({
          appId      : fb_app_id,
          cookie     : true,
          xfbml      : true,
          version    : 'v2.8'
        });

        FB.getLoginStatus(function(response) {
            if (response.status == 'connected') {
                    onLogin(response);
                } else {
                FB.login(function(response) {
                    onLogin(response);
                }, {scope: 'user_friends, email'});
            }
        });
    }
    else{
        Rollbar.debug("Not loaded base SDK");
        return false;
    }
};

(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

function checkLoginState() {

    if(typeof FB == 'undefined'){
        Rollbar.debug("FB is undefined. Was a problem loading the Facebook resources. Maybe your provider temporarily blocks this resource");
        return false;
    }

    FB.getLoginStatus(function(response) {
        onLogin(response);
    });
}
4

1 回答 1

0

我们有同样的问题。
Facebook 的代码中似乎存在错误。SDK 内部 -

                 var i = "fbNativeReady";
                 a = {
                    onready: function(a) {
                        __p && __p();
                        if (!h.nativeApp()) {
                            g.error("FB.Native.onready only works when the page is rendered in a WebView of the native Facebook app. Test if this is the case calling FB.UA.nativeApp()");
                            return
                        }
                        window.__fbNative && !this.nativeReady && ES("Object", "assign", !1, this, window.__fbNative);
                        if (this.nativeReady) a();
                        else {
                            var b = function b(c) {
                                window.removeEventListener(i, b), this.onready(a)
                            };
                            window.addEventListener(i, b, !1)
                        }
                    }
                };

可以看到事件监听器“b”没有被绑定,所以当事件“fbNativeReady”被调度时,监听器在没有上下文的情况下被调用,这意味着“this”是窗口,并且不存在“onready”函数。
将 b 绑定到“this”可以解决此问题。

一种可能的解决方案是在一定延迟后运行此代码,以避免在加载代码后调度 fbNativeReady 事件

于 2019-02-10T09:25:36.103 回答