【发布时间】:2018-01-08 03:52:51
【问题描述】:
我有一个名为“scores”的表,想要过滤 UserId 并按他们的分数排序。我想为我的 Discord Bot 创建一个排名列表命令,但我不知道该怎么做。
我虽然是这样但它不起作用:
const sql = require("sqlite");
sql.open("score.sqlite");
exports.run = (client, message, args) => {
var index = 1;
var msg = "-- Top 5 list --\n";
sql.get("SELECT * FROM scores ORDER BY points DESC LIMIT 5").then(rows => {
for (index = 1; index < 6; index++) {
if (rows[index] !== null) {
msg += index + ". " + rows[index].userId + " - " + rows[index].points + "\n";
}
}
console.log(msg);
});
};
你们能帮帮我吗?
谢谢。
【问题讨论】:
-
请详细说明“它不起作用”。
-
没有错误。而在控制台中也没什么。
-
您似乎没有发现错误。尝试将错误处理程序添加到您的
.get和/或看看您是否至少获得了您的查询。 -
我收到以下错误“TypeError: Cannot read property 'userId' of undefined” 但我不明白为什么。
-
看看你在
rows中得到了什么。undefined和null在 JavaScript 中是不同的东西,只有在rows数组中有明确的null值时,您的 null-guardif语句才有效。您可以简单地检查if(rows[index])以保证有一个真实的值。
标签: javascript sqlite discord.js