this is the code I have for my warning command, it is reading from a sqlite database.
user = message.mentions.users.first()
embed = new Discord.MessageEmbed()
.setTitle(`warnings of ${user.username}`)
.setTimestamp()
.setFooter('shadowcraft - **Test Mode!!**');
sql = 'SELECT * from moderation WHERE userID = ?'
db.each(sql, [user.id], (err, row) => {
if (err) throw err;
mod = client.user.fetch(toString(row.moderatorID))
console.log(row)
embed.addField(`${mod.tag}`, `${row.reason} \n${row.date}`);
});
message.channel.send(embed)
When I run it, it prints the row to the console but the embed is empty and has no fields. The problem is that it sends the embed before it gets the results from db.each()
db.each is a callback and not a promise so I cant use await .then() etc
Is there a way to make the callback into a promise so that I can use an async function
this is the full console log, there are no errors etc only the rows
{
id: 1,
date: '2020-04-02',
userID: 'some user id',
action: 'WARN',
reason: 'some reason',
moderatorID: 'some user id'
}
{
id: 2,
date: '2020-04-02',
userID: 'some user id',
action: 'WARN',
reason: 'some reason',
moderatorID: 'some user id'
}
WHERE userID = ${message.author.id}? It makes it easier, also, the ‘from’ has to be in caps, so it would beSELECT * FROM moderation WHERE userID = ?- you don’t have to use the ? Here as you are not vulnerable to sql injection because the user cannot change values in the table remotely