Javascript 从使用d3.js创建的对象获取未定义的属性

Javascript 从使用d3.js创建的对象获取未定义的属性,javascript,d3.js,Javascript,D3.js,我正在尝试从csv文件读取数据,并将返回的数据记录在console.log中。我越来越不确定了。为什么如果我在console.log中记录store对象,我可以在控制台中看到routes的属性?但是尝试控制台记录routes属性时显示undefined let store = {}; function loadData() { // Add the code to load the CSV file named "routes.csv" | 1 Line let promise

我正在尝试从csv文件读取数据,并将返回的数据记录在console.log中。我越来越不确定了。为什么如果我在console.log中记录store对象,我可以在控制台中看到routes的属性?但是尝试控制台记录routes属性时显示undefined

let store = {};

function loadData() {
    // Add the code to load the CSV file named "routes.csv" | 1 Line
    let promise = d3.csv("routes.csv");

    return promise.then(routes => {
        // Save the routes into our store variable;
        store.routes = routes;
        return store;
    })

}

loadData();
console.log(store.routes);
csv文件的前几行如下所示:

ID,AirlineID,AirlineName,AirlineCountry,SourceAirportID,SourceAirportCode,SourceAirport,SourceCity,SourceCountry,SourceLatitude,SourceLongitude,DestAirportID,DestCode,DestAirport,DestCity,DestCountry,DestLatitude,DestLongitude
1,24,American Airlines,United States,4355,ABE,Lehigh Valley International Airport,Allentown,United States,40.65209961,-75.44080353,3876,CLT,Charlotte Douglas International Airport,Charlotte,United States,35.2140007,-80.94309998
2,24,American Airlines,United States,4355,ABE,Lehigh Valley International Airport,Allentown,United States,40.65209961,-75.44080353,3752,PHL,Philadelphia International Airport,Philadelphia,United States,39.87189865,-75.2410965
3,24,American Airlines,United States,3718,ABI,Abilene Regional Airport,Abilene,United States,32.41130066,-99.68190002,3670,DFW,Dallas Fort Worth International Airport,Dallas-Fort Worth,United States,32.896801,-97.03800201
4,24,American Airlines,United States,4019,ABQ,Albuquerque International Sunport Airport,Albuquerque,United States,35.04019928,-106.6090012,3670,DFW,Dallas Fort Worth International Airport,Dallas-Fort Worth,United States,32.896801,-97.03800201
5,24,American Airlines,United States,4019,ABQ,Albuquerque International Sunport Airport,Albuquerque,United States,35.04019928,-106.6090012,3484,LAX,Los Angeles International Airport,Los Angeles,United States,33.94250107,-118.4079971
6,24,American Airlines,United States,4019,ABQ,Albuquerque International Sunport Airport,Albuquerque,United States,35.04019928,-106.6090012,3830,ORD,Chicago O'Hare International Airport,Chicago,United States,41.97859955,-87.90480042
7,24,American Airlines,United States,4019,ABQ,Albuquerque International Sunport Airport,Albuquerque,United States,35.04019928,-106.6090012,3462,PHX,Phoenix Sky Harbor International Airport,Phoenix,United States,33.43429947,-112.012001
8,24,American Airlines,United States,532,ABZ,Aberdeen Dyce Airport,Aberdeen,United Kingdom,57.20190048,-2.197779894,507,LHR,London Heathrow Airport,London,United Kingdom,51.4706,-0.461941

在then块中返回值将返回一个承诺。因此,您需要在函数调用中添加一个then块,这在这里是不需要的。我认为这只会让事情变得更复杂

只需将数据记录在函数中,然后将代码写入
块中即可

let store = {};

function loadData() {
    // Add the code to load the CSV file named "routes.csv" | 1 Line
    let promise = d3.csv("routes.csv");

promise.then(routes => {
    // Save the routes into our store variable;
    store.routes = routes;
    console.log(store.routes);
  })
}

loadData();
更新

let store = {};

function loadData() {
    // Add the code to load the CSV file named "routes.csv" | 1 Line
    let promise = d3.csv("routes.csv");
    return promise;
}

loadData().then(routes => {
  // Save the routes into our store variable;
  store.routes = routes;
  console.log(store.routes);
})

在then块中返回值将返回一个承诺。因此,您需要在函数调用中添加一个then块,这在这里是不需要的。我认为这只会让事情变得更复杂

只需将数据记录在函数中,然后将代码写入
块中即可

let store = {};

function loadData() {
    // Add the code to load the CSV file named "routes.csv" | 1 Line
    let promise = d3.csv("routes.csv");

promise.then(routes => {
    // Save the routes into our store variable;
    store.routes = routes;
    console.log(store.routes);
  })
}

loadData();
更新

let store = {};

function loadData() {
    // Add the code to load the CSV file named "routes.csv" | 1 Line
    let promise = d3.csv("routes.csv");
    return promise;
}

loadData().then(routes => {
  // Save the routes into our store variable;
  store.routes = routes;
  console.log(store.routes);
})

王子,谢谢你的回复。是否可以在loadData函数外部控制台记录数据,以测试loadData函数是否工作。这就是我最初试图做的,以测试我的loadData函数是否工作。我已经编辑了我的解决方案,这是您可以在loadData之外使用它的方式,但您只能在then块内对其进行控制台。Prince,感谢您的回复。是否可以在loadData函数外部控制台记录数据,以测试loadData函数是否工作。这就是我最初试图做的,以测试我的loadData函数是否工作。我已经编辑了我的解决方案,这是在loadData之外使用它的方式,但您只能在then块内对它进行控制台操作。