簡體   English   中英

將 elasticsearch 與 d3.js 集成時,“Uncaught TypeError: Cannot read property 'Client' of undefined” 我收到此錯誤

[英]When integrate elasticsearch with d3.js, “Uncaught TypeError: Cannot read property 'Client' of undefined” I get this error

我正在嘗試成為使用 elasticsearch.js API 的客戶端。 我的目標是成為 Elasticsearch 的客戶,並嘗試將數據轉換為我自己網頁上的圖形。 然后我得到了這樣的錯誤。 如何解決這個問題?

或者你能給我另一個來源或例子嗎?

索引.xhtml

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Elastic Aggregations</title>
        <script src="scripts/require.js"></script>
        <script>require(["scripts/main"], function () {});</script>
        <style>
            body {
                font: 14px sans-serif;
            }
            .arc path {
                stroke: #fff; 
                stroke-width: 3px;
            }
        </style>
    </head>
    <body>
        <div id="donut-chart"></div>
    </body>
</html>

主文件

define(['scripts/d3.v3', 'scripts/elasticsearch'], function (d3, elasticsearch) {
    "use strict";
    var client = new elasticsearch.Client({
        host: 'http://localhost:9200',
        log: 'trace'
    });
    client.search({
        index: 'nfl',
        size: 5,
        body: {
            // Begin query.
            query: {
                // Boolean query for matching and excluding items.
                bool: {
                    must: {match: {"description": "TOUCHDOWN"}},
                    must_not: {match: {"qtr": 5}}
                }
            },
            // Aggregate on the results
            aggs: {
                touchdowns: {
                    terms: {
                        field: "qtr",
                        // order by quarter, ascending
                        order: {"_term": "asc"}
                    }
                }
            }
            // End query.
        }
    }).then(function (resp) {
        console.log(resp);
        // D3 code goes here.
        var touchdowns = resp.aggregations.touchdowns.buckets;
        // d3 donut chart
        var width = 600,
                height = 300,
                radius = Math.min(width, height) / 2;
        var color = ['#ff7f0e', '#d62728', '#2ca02c', '#1f77b4'];
        var arc = d3.svg.arc()
                .outerRadius(radius - 60)
                .innerRadius(120);
        var pie = d3.layout.pie()
                .sort(null)
                .value(function (d) {
                    return d.doc_count;
                });
        var svg = d3.select("#donut-chart").append("svg")
                .attr("width", width)
                .attr("height", height)
                .append("g")
                .attr("transform", "translate(" + width / 1.4 + "," + height / 2 + ")");
        var g = svg.selectAll(".arc")
                .data(pie(touchdowns))
                .enter()
                .append("g")
                .attr("class", "arc");
        g.append("path")
                .attr("d", arc)
                .style("fill", function (d, i) {
                    return color[i];
                });
        g.append("text")
                .attr("transform", function (d) {
                    return "translate(" + arc.centroid(d) + ")";
                })
                .attr("dy", ".35em")
                .style("text-anchor", "middle")
                .style("fill", "white")
                .text(function (d) {
                    return d.data.key;
                });
    });
});

和目錄

|
|-scripts
|    |-main.js
|    |-elasticsearch.js  
|    |-require.js
|    |-d3.v3.js
|
|-index.xhtml

和錯誤信息;

Uncaught TypeError: Cannot read property 'Client' of undefined
    at VM899 main.js:3
    at Object.execCb (require.js:1650)
    at Module.check (require.js:866)
    at Module.<anonymous> (require.js:1113)
    at require.js:132
    at require.js:1156
    at each (require.js:57)
    at Module.emit (require.js:1155)
    at Module.check (require.js:917)
    at Module.enable (require.js:1143)

看來您使用的是 Node 版本。

嘗試導入這個: https : //www.npmjs.com/package/elasticsearch-browser

暫無
暫無

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

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