Search code examples
javascriptgoogle-mapstypeerrortypeofadblock

How to catch Uncaught typeerror: cannot read property 'datatable' of undefined. error


When I turn off Google API I get an "Uncaught TypeError: Cannot read property 'DataTable' of undefined" message.

I would like to catch this undefined error and have a window popping up saying "Google has been disabled" but I'm not sure what typeof Object is undefined. I'm currently checking "DataTable" Object but I'm pretty sure this is wrong as the window pops up regardless now.

Here is my code.

var data = new google.visualization.DataTable();
if(typeof DataTable == "undefined"){
window.alert("sometext");
} else {
data.addColumn('string', 'Name');
data.addColumn('string', 'Manager');
data.addColumn('string', 'ToolTip');
data.addColumn('string', 'Type');
data.addColumn('string', 'Code');
data.addColumn('string', 'Aggregation');
data.addColumn('string', 'Stage');

data.addRows([
<xsl:apply-templates select="/*" mode="render"/>
['1','2','3','4','5','6','7']
]);

data.removeRow(data.getNumberOfRows() - 1);

drawDebugFlow(data);
}

Solution

  • instead of:

    if(typeof DataTable == "undefined"){
    

    use:

    if(typeof google == "undefined" || typeof google.visualization == "undefined"){
    

    Error is "Uncaught TypeError: Cannot read property 'DataTable' of undefined" not "Datatable is undefined"