User being displayed as [object Object]

Hey, I'm trying to display a leaderboard of users having the most gold saved in my sqlite database, here's my command:
async execute(interaction) {
const private = interaction.options.getBoolean(OPTION_PRIVATE);
const usersAmount = interaction.options.getInteger(OPTION_AMOUNT_OF_USERS);

const users = await Users.findAll({
order: [['balance', 'DESC']],
limit: usersAmount,
});
if (users.length === 0) {
await interaction.reply({ content: 'Everyone is broke :(', ephemeral: private });
}
else {
let message = '';
const limit = users.length < usersAmount ? users.length : usersAmount;

const rest = new REST().setToken(process.env.DISCORD_TOKEN);
const fetchUser = async id => rest.get(Routes.user(id));

for (let i = 0; i < limit; i++) {
const user = await fetchUser(users[i].user_id).catch(() => null);
message += `${i + 1}. ${user ? user.global_name : 'Unknown user'} - ${users[i].balance} gold.\n`;
}
await interaction.reply({ content: message, ephemeral: private });
}
},
async execute(interaction) {
const private = interaction.options.getBoolean(OPTION_PRIVATE);
const usersAmount = interaction.options.getInteger(OPTION_AMOUNT_OF_USERS);

const users = await Users.findAll({
order: [['balance', 'DESC']],
limit: usersAmount,
});
if (users.length === 0) {
await interaction.reply({ content: 'Everyone is broke :(', ephemeral: private });
}
else {
let message = '';
const limit = users.length < usersAmount ? users.length : usersAmount;

const rest = new REST().setToken(process.env.DISCORD_TOKEN);
const fetchUser = async id => rest.get(Routes.user(id));

for (let i = 0; i < limit; i++) {
const user = await fetchUser(users[i].user_id).catch(() => null);
message += `${i + 1}. ${user ? user.global_name : 'Unknown user'} - ${users[i].balance} gold.\n`;
}
await interaction.reply({ content: message, ephemeral: private });
}
},
Works fine with user.global_name but displaying just the user results in [object Object] in the message. Why is that? I thought that printing out user in the string literal or using .toString() method will automatically display the user as mention @username. What am I missing?
5 Replies
d.js toolkit
d.js toolkit17mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Syjalo
Syjalo17mo ago
<User>.toString() returns '<@id>', where User is a discord.js class instanse (structure). Your fetchUser function returns a raw object, not the User class instanse.
Roirraw
RoirrawOP17mo ago
Thanks @syjalo. Is there a way to convert such object into user class instance? Or can I fetch the user in a different way to get the user class instance?
Syjalo
Syjalo17mo ago
The User class doesn't has .globalName property yet. And you can just do
`${userMention(user.id)}`;
// or
`<@${user.id}>`;
`${userMention(user.id)}`;
// or
`<@${user.id}>`;
Roirraw
RoirrawOP17mo ago
let me try! Works like charm, thanks a lot! :)
Want results from more Discord servers?
Add your server