未捕获的TypeError:无法调用null的方法'shift'

时间:2013-01-28 22:20:16

标签: facebook facebook-javascript-sdk requirejs

我只是在Chrome中收到此消息。 Firefox工作得很好。我一直在调试试图找出这个错误发生在什么时候,似乎无法确定它。确切的消息是,“未捕获的TypeError:无法从all.js调用方法'shift'为空”:57。

此外,我还会注意到其他一些可能对您有帮助的事情:

  • 我正在将SDK加载为requirejs模块。我也试过加载“正常方式”但结果是一样的。
  • 它不是在本地发生的,只在我们的登台服务器上发生。这让我觉得它在SDK加载期间有一些延迟。
  • 这只发生在Chrome中。 Firefox和Safari一样有效。 Safari中唯一的区别是我看到类似的错误,说明这是一个类型问题。那里的消息是“'null'不是一个对象(评估'queue.shift`)”@ debug.js:2712但它不会破坏应用程序。

这里的任何帮助和/或见解都会很棒。

4 个答案:

答案 0 :(得分:1)

重写调用SDK脚本对我有所帮助。它需要被调用,因为它被解释为on facebook

首先,我尝试使用id ='fasebook-jssdk'将脚本附加到head元素,但是发生了方法'shift'的错误。然后我重写了它,因为它是由facebook推荐的,没有FB.init()。但是在我添加FB.init()调用之前,错误一直在发生。现在它可以正常工作。

试图解决它,我找到了'all.js'file的调试版本,我发现在以下代码中发生错误:

function flush() {
  if (!queue) {
    return;
  }

  var fn;
  while (fn = queue.shift()) {
    fn();
  }
  queue = null;
}

其中queue是一个函数数组。我不知道原因,但现在,当脚本运行正常时,我真的不需要。也许比我更谨慎的人会发现。

答案 1 :(得分:1)

我有类似的问题。这解决了我的问题。这些是症状:

  1. localhost上没有问题
  2. 在服务器(Amazon S3)偶尔会收到“未捕获的TypeError:无法调用方法'shift'的null”
  3. 解决方案:

    我搞砸了Facebook SDK的包含顺序和fbAsyncInit的处理程序。在包含用于处理SDK加载的脚本之后,应该包括SDK。否则,SDK可能会在其加载事件的处理程序已注册之前加载。

    <script src="Your script with fbAsyncInit here"> </script>
       <div id="fb-root"></div>
        <script>(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 = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=200817623450198";
          fjs.parentNode.insertBefore(js, fjs);
          }(document, 'script', 'facebook-jssdk'));
        </script>
      <div id="fb-root"></div>
      </body>
    

答案 2 :(得分:0)

对不起,我可以提供很多帮助,但我有同样的问题可能有助于解决这个问题。

加载JS likeo:

<script src='//connect.facebook.net/en_US/all.js'></script> 
  

“它不是在本地发生的,只在我们的登台服务器上发生。这让我感到高兴   认为它在SDK加载期间有一些延迟。“

我倾向于同意。该错误似乎只是零星发生。另外,我偶尔会遇到CURLOPT_IPRESOLVE未定义的错误。似乎所有都与连接有关。

答案 3 :(得分:0)

这些Facebook Instructions解释了调用SDK的不同方式,这对我有用。我还没有测试过它,但我认为通道文件可以让你在本地进行测试,这非常甜蜜。

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    // init the FB JS SDK
    FB.init({
      appId      : 'YOUR_APP_ID', // App ID from the App Dashboard
      channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File for x-domain communication
      status     : true, // check the login status upon init?
      cookie     : true, // set sessions cookies to allow your server to access the session?
      xfbml      : true  // parse XFBML tags on this page?
    });

    // Additional initialization code such as adding Event Listeners goes here

  };

  // Load the SDK's source Asynchronously
  // Note that the debug version is being actively developed and might 
  // contain some type checks that are overly strict. 
  // Please report such bugs using the bugs tool.
  (function(d, debug){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
     ref.parentNode.insertBefore(js, ref);
   }(document, /*debug*/ false));
</script>