Answer the question
In order to leave comments, you need to log in
Why empty array after query to sqlite database?
// players = [ ]
try {
let players = [];
const kills = message.guild.emojis.cache.find(emoji => emoji.name === "kills"),
damage = message.guild.emojis.cache.find(emoji => emoji.name === "damage");
return new Promise(resolve => {
channel.members.map(async (member) => {
await client.db.get(`SELECT * FROM players WHERE user_id='${member.id}'`, (err, player) => {
if (player) {
players.push(`[**${player.level}**] ${member} ${player.kills ? `${kills} **${player.kills}** ` : ''}${player.damage ? `${damage} **${player.damage}**` : ''}`)
} else {
players.push(`[**-**] ${member}`)
}
})
})
resolve(players)
})
Answer the question
In order to leave comments, you need to log in
at least this is fundamentally wrong
here is not the correct use of map as forEach - but this is not so bad ...
as you would expect: inside the map you make an asynchronous request to the base, get the player, put it in an array. After processing the entire array, call resolve(players)
what happens: map runs through members, a promise with a request starts for each, resolve() is executed - it returns an empty array, because none of the promises have resolved yet
Replace map with for ... of
channel.members.map(async (member) => {})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question