字符串切片不是功能D3仪表板

时间:2019-03-13 18:06:06

标签: javascript node.js mongodb d3.js mern

使用awsconsole统计数据构建D3仪表板。数据位于MongoDB中,并且在路由到浏览器时似乎已正确加载。

但是,当尝试通过D3仪表板加载数据时,控制台中会产生以下错误

Uncaught TypeError: **string.slice is not a function**
at **d3_time_parseFullYear (d3.js:2716)**
at d3_time_parse (d3.js:2519)
at Function.format.parse (d3.js:2495)

尝试了多种格式,但没有成功,仪表盘加载失败,但是由于切片错误而没有数据

示例queue.js代码段

queue()
    .defer(d3.json, "/donorschoose/projects")
    .defer(d3.json, "static/geojson/us-states.json")
    .await(makeGraphs);

function makeGraphs(error, projectsJson, statesJson) {

    //Clean projectsJson data
    var donorschooseProjects = projectsJson;
    // var dateFormat = d3.time.format("%Y-%m-%d");
    var dateFormat = d3.time.format("%Y-%m-%d %H:%M:%S");
    // var dateFormat = d3.time.format("%Y-%m-%d %X");
    donorschooseProjects.forEach(function(d) {
        d["date_posted"] = dateFormat.parse(d["date_posted"]);
        d["date_posted"].setDate(1);
        d["total_donations"] = +d["total_donations"];
    });

示例Mongo数据(带时间戳)

{
"_id" : ObjectId("5c8933d131a8834ae488c4d6"),
"date_posted" : ISODate("2003-06-18T00:00:00Z"),
"date_completed" : ISODate("2004-01-23T00:00:00Z"),
"date_thank_you_packet_mailed" : ISODate("2004-02-26T00:00:00Z"),
"date_expiration" : ISODate("2004-06-16T00:00:00Z")
}

示例D3.js代码段

}
  function d3_time_parseWeekNumberMonday(date, string, i) {
    d3_time_numberRe.lastIndex = 0;
    var n = d3_time_numberRe.exec(string.slice(i));
    return n ? (date.W = +n[0], i + n[0].length) : -1;
  }
  function d3_time_parseFullYear(date, string, i) {
    d3_time_numberRe.lastIndex = 0;
    var n = d3_time_numberRe.exec(string.slice(i, i + 4));
    return n ? (date.y = +n[0], i + n[0].length) : -1;
  }
  function d3_time_parseYear(date, string, i) {
    d3_time_numberRe.lastIndex = 0;
    var n = d3_time_numberRe.exec(string.slice(i, i + 2));
    return n ? (date.y = d3_time_expandYear(+n[0]), i + n[0].length) : -1;
  }
  function d3_time_parseZone(date, string, i) {
    return /^[+-]\d{4}$/.test(string = string.slice(i, i + 5)) ? (date.Z = -string, 
    i + 5) : -1;
  }
  function d3_time_expandYear(d) {
    return d + (d > 68 ? 1900 : 2e3);
  }

0 个答案:

没有答案