markmarkoh / datamaps

Customizable SVG map visualizations for the web in a single Javascript file using D3.js

Home Page:http://datamaps.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't call Datamaps from Async function

Gonzalo2712 opened this issue · comments

Hi.

Im trying to populate a worldmap using data from API. To load the data, in my script i use async functions, the probles is that when i try to create a map inside async function like this:

async function fetchData() { try { const response = await fetch('https://corona.lmao.ninja/v2/countries?yesterday=&sort='); const data = await (response.json()); var map = new Datamap({element: document.getElementById('container')}); return data; } catch (error) { console.error(error); } } fetchData();

I get this error:

test.js:10 TypeError: Cannot read property 'select' of undefined at Datamap.drawSubunits (datamaps.all.js:184) at draw (datamaps.all.js:789) at Datamap.draw (datamaps.all.js:768) at new Datamap (datamaps.all.js:730)

But if i call it the same way but outside the function it works.

Any help will be appreciated.