我试图简单地获取存储在 mongoDB 中的数据并将其推送到 HTML 中的表中。我需要访问 hangman 数据库 > players 集合,其中仅包含 name 和 score。请有人指出我哪里出错了?
但是我的 Chrome 控制台错误是:Cannot read property 'length' of undefined
.
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>
server.js
app.get("/", function(request, response) {
response.status(200).sendFile("/", {root: "client"});
});
最佳答案
请设置正确的获取数据的路径
$.get("localhost:9000/getPlayers", {}, function (res) {
...
});
关于javascript - 尝试从我的 mongoDB 访问数据并推送到 HTML 中的 <table>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59546666/