如何解决Uncaught TypeError:对象不是函数?

时间:2014-03-05 04:52:37

标签: javascript

我的javascript代码中出现此错误。当我在图表和地图上单独工作时,它工作正常。但是,当我整合这两个时,它就停止了工作。

<html>
<head>
  <script type='text/javascript' src='https://www.google.com/jsapi'></script>
  <script type='text/javascript'>
   google.load('visualization', '1', {'packages': ['geomap']});
   google.setOnLoadCallback(drawMap);
    var test;

    function drawMap() {
        var test1 = {{pie2008|safe}}
        var test2 = {{pie2009|safe}}
        var test3 = {{pie2010|safe}}

        console.log(test1);
        console.log(test2);
        console.log(test3);
    }

    function drawMap2008() {
        console.log("In draw map function");
        var data = google.visualization.arrayToDataTable({{data2008|safe}});
        var options = {};
        options['region'] = 'US'
        options['dataMode'] = 'regions';
        var container = document.getElementById('map_canvas');
        var geomap = new google.visualization.GeoMap(container);
        console.log(data);
        geomap.draw(data, options);
    }

    function drawMap2009() {
        console.log("In draw map function");
        var data = google.visualization.arrayToDataTable({{data2009|safe}});
        var options = {};
        options['region'] = 'US'
        options['dataMode'] = 'regions';
        var container = document.getElementById('map_canvas');
        var geomap = new google.visualization.GeoMap(container);
        console.log(data);
        geomap.draw(data, options);
    }

    function drawMap2010() {
        console.log("In draw map function");
        var data = google.visualization.arrayToDataTable({{data2010|safe}});
        var options = {};
        options['region'] = 'US'
        options['dataMode'] = 'regions';
        var container = document.getElementById('map_canvas');
        var geomap = new google.visualization.GeoMap(container);
        console.log(data);
        geomap.draw(data, options);
    } 

    function drawChart2008() {
        var data = google.visualization.arrayToDataTable({{pie2008|safe}});
        var options = {
          title: 'Maximum Facebook users for 2008',
          pieHole: 0.4,
        };      
        var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
        chart.draw(data, options);
      }

    function drawChart2009() {
          console.log("In draw map function");
        var data = google.visualization.arrayToDataTable({{pie2009|safe}});
        var options = {
          title: 'Maximum Facebook users for 2009',
          pieHole: 0.4,
        };      
        var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
        chart.draw(data, options);
      }

      function drawChart2010() {
          console.log("In draw map function");
        var data = google.visualization.arrayToDataTable({{pie2010|safe}});
        var options = {
          title: 'Maximum Facebook users for 2010',
          pieHole: 0.4,
        };      
        var container2 = document.getElementById('donutchart');
        var chart = new google.visualization.PieChart(container2);
        chart.draw(data, options);
      }

  </script>
</head>

<body>
  <div id="handb">
    <h3>No of facebook users per state</h3>
    <button id="data2008" onclick = "drawMap2008()">Intensity Map for 2008</button>
    <button id="data2009" onclick = "drawMap2009()">Intensity Map for 2009</button>
    <button id="data2010" onclick = "drawMap2010()">Intensity Map for 2010</button>
    <button id="pie2008" onclick = "drawChart2008()">Pie chart for 2008</button>
    <button id="pie2009" onclick = "drawChart2009()">Pie chart for 2009</button>
    <button id="pie2010" onclick = "drawChart2010()">Pie chart for 2010</button>
  </div>
  <div id='map_canvas'></div>
  <div id='donutchart'></div>
</body>

</html>

我不明白为什么我会收到此错误。我在drawchart2008函数中得到这个错误 -  var chart = new google.visualization.PieChart(document.getElementById('donutchart'));             chart.draw(数据,选项);

1 个答案:

答案 0 :(得分:0)

我怀疑chart.draw(data, options);实际上并不是一个函数(.draw()不是类型chart上的方法)。
仅仅因为geomap.draw(data, options);上存在.draw()并不意味着它对chart有效。