簡體   English   中英

從使用 d3.js 創建的對象中獲取未定義的屬性

[英]Getting Undefined property from Object created with d3.js

我正在嘗試從 csv 文件和 console.log 中讀取數據返回的數據。 我越來越不確定了。 為什么如果我 console.log 存儲對象我可以在控制台中看到路由的屬性? 但是嘗試控制台記錄 routes 屬性顯示未定義。

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 塊,而此處不需要。 我認為這只會讓它變得更加復雜。

您可以簡單地記錄函數內的數據,並僅在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);
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM