简体   繁体   English

使用异步函数的DataMaps js问题

[英]DataMaps js problem using async functions

I've created a script that gets data from API, format the data and use it using DataMaps http://datamaps.github.io/ .我创建了一个从 API 获取数据的脚本,格式化数据并使用 DataMaps http://datamaps.github.io/使用它。

In my.js file i can simply call:在 my.js 文件中,我可以简单地调用:

var map = new Datamap({element: document.getElementById('container')});

And it will render the map in selected div.它将在选定的 div 中呈现 map。 The problem is that if i use it inside async function to get the data from an API and render the map filled with API's data I get this error:问题是,如果我在异步 function 中使用它来从 API 获取数据并渲染充满 API 数据的 map,我会收到此错误:

Uncaught (in promise) 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)
...

I even get this error if i just call the map this way inside the async function, so the data is not the problem:如果我只是在异步 function 中以这种方式调用 map,我什至会收到此错误,因此数据不是问题:

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();

If I put var map = new Datamap({element: document.getElementById('container')});如果我把var map = new Datamap({element: document.getElementById('container')}); outside the function it works.在 function 之外它可以工作。 I have no clue about it.我对此一无所知。

Any help would be appreciated.任何帮助,将不胜感激。 First post and english is not my main language.第一篇文章和英语不是我的主要语言。

judging by mistake, you use the nonexistent variable 'select' or an empty variable.错误地判断,您使用了不存在的变量“选择”或空变量。 maybe I was wrong in spelling my English level is not so good也许我拼错了我的英语水平不是很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM