Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试从我的mongoDB访问数据并推送到a<;表>;在HTML中_Javascript_Jquery_Node.js_Mongodb - Fatal编程技术网

Javascript 尝试从我的mongoDB访问数据并推送到a<;表>;在HTML中

Javascript 尝试从我的mongoDB访问数据并推送到a<;表>;在HTML中,javascript,jquery,node.js,mongodb,Javascript,Jquery,Node.js,Mongodb,我试图简单地获取存储在mongoDB中的数据,并将其推送到HTML表中。我需要访问刽子手数据库>玩家集合,其中只包含姓名和分数。有人能指出我哪里出了错吗 但是,我的Chrome控制台错误是:无法读取未定义的属性“length” leadership.html页面代码 <table> <thead> <th>NAME</th><th>SCORE</th></tr> <

我试图简单地获取存储在mongoDB中的数据,并将其推送到HTML表中。我需要访问刽子手数据库>玩家集合,其中只包含姓名分数。有人能指出我哪里出了错吗

但是,我的Chrome控制台错误是:
无法读取未定义的属性“length”

leadership.html页面代码

<table>
    <thead>
        <th>NAME</th><th>SCORE</th></tr>
        </thead>
    <tbody id="leadership"></tbody>
</table>  



<script>
    $(function() {
        $.get("http://localhost:9000/", {}, function (res) {
        //let data = res;
        const { data } = res;
            for (i = 0; i < data.length; i++) {
                let name = data[i].name;
                let score = data[i].score;
                //let score = score[score.length - 1];

                console.log(data[i].name);
                console.log(res);

                $("#leadership").append("<tr><td class=\"name\">"
                    + data[i].name + "</td><td class=\"score\">" 
                    + data[i].score + "</td></tr>");
                }
            });
    });
</script>
控制台的错误消息
第69行:引用(i=0;i{

在初始化之前尝试使用score

let score = score[score.length - 1];
我猜您想使用data.score。

问题是

let score = score[score.length - 1];
您将score定义为score的一个元素(您没有定义)

你想把分数定义成什么

而且,似乎两者都有

let name = data[i].name;
let score = score[score.length - 1];

无法删除,因为您正在添加数据[i].name/data[i].score的数据,因此无需定义此项。

请设置正确的获取数据路径

  $.get("localhost:9000/getPlayers", {}, function (res) {
   ...
  });

您好,所以我尝试用
let score=data[i].score;
替换这一行,我得到了表中显示的
undefined
  $.get("localhost:9000/getPlayers", {}, function (res) {
   ...
  });