我想为我的 Discord 用户创建一个排行榜。
这是我的代码:
const Discord = require('discord.js')
const sql = require("sqlite")
sql.open("../score.sqlite")
exports.run = (client, message, args) => {
sql.get("SELECT * FROM scores GROUP BY userId ORDER BY points DESC LIMIT 10")
.then(rows => {
let embed = new Discord.RichEmbed()
.setFooter('Bot')
.setTimestamp()
let userArray = []
let moneyArray = []
rows.forEach(row => {
userArray.push(row.userId)
moneyArray.push(row.points)
})
embed.addField('Name', userArray.join('\n'), true)
embed.addField('Money', moneyArray.join('\n'), true)
message.channel.send({embed})
})
}
我不明白为什么 forEach 没有函数。
最佳答案
假设您使用的sqlite库是this Promise-adding wrapper , the documentation从 sql.get
返回的值的状态:
If the result set is empty, [it] is
undefined
, otherwise it is an object containing the values for the first row. The property names correspond to the column names of the result set.
由于您的 rows
参数是一个对象,因此没有 forEach 函数。它也将是一个包含单行值的对象 - 如果您想从数据库获取整个行集,您应该使用 sql.all 而不是 sql.get >.
使用 Object.keys
的效果如下:
Object.keys(rows).forEach(key => {
userArray.push(rows[key].userId);
moneyArray.push(rows[key].points);
});
关于javascript - rows.forEach 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45506492/