为什么我在airconsole-1.4.0.js:669得到“Uncaught TypeError”?

时间:2017-09-26 10:41:08

标签: javascript html airconsole

我是空气控制台的新手,我只是学习它,所以也许这是常见的事情。 我在screen.html上有这个:

<html>
  <head>
    <script type="text/javascript" src="https://www.airconsole.com/api/airconsole-1.7.0.js"></script>
    <script type="text/javascript">
    var air_console = new AirConsole();

    // Listen for messages from other devices
    air_console.onMessage = function(from, data) {

      // We receive a message -> Send message back to the device
      air_console.message(from, "Full of pixels!");

      // Show message on device screen
      var info = document.createElement('DIV');
      info.innerHTML = data;
      document.body.appendChild(info);
    };
    </script>
    <style type="text/css">
      color: white;
    </style>
  </head>
  <body>

  </body>
</html>

我在controller.html中有这个

<html>
<head>
  <script type="text/javascript" src="https://www.airconsole.com/api/airconsole-1.7.0.js"></script>
  <script type="text/javascript">
    var air_console = new AirConsole();

    // Send a message to the screen
    air_console.message(AirConsole.SCREEN, "How are you?");

    // Listen for messages
    air_console.onMessage = function(from, data) {

      // Show message on device screen
      var info = document.createElement('DIV');
      info.innerHTML = data;
      document.body.appendChild(info);
    };
  </script>
    <style type="text/css">
      color: white;
    </style>
</head>
<body>

</body>
</html>

我在这两个方面都遇到了这个错误:

Uncaught TypeError: Cannot read property 'undefined' of undefined
    at AirConsole.onPostMessage_ (airconsole-1.7.0.js:1012)
    at airconsole-1.7.0.js:927

当然屏幕上什么都没有。这里我的错误是什么,我该如何解决?

1 个答案:

答案 0 :(得分:0)

在发送消息之前,您需要等待 onReady 事件:

<html>
<head>
  <script type="text/javascript" src="https://www.airconsole.com/api/airconsole-1.7.0.js"></script>
  <script type="text/javascript">
    var air_console = new AirConsole();

    air_console.onReady = function() {
      // Send a message to the screen
      air_console.message(AirConsole.SCREEN, "How are you?");
    }

    // Listen for messages
    air_console.onMessage = function(from, data) {

      // Show message on device screen
      var info = document.createElement('DIV');
      info.innerHTML = data;
      document.body.appendChild(info);
    };
  </script>
    <style type="text/css">
      color: white;
    </style>
</head>
<body>

</body>
</html>