Cannot read properties of undefined (reading 'length') discord.js

Attached below is my code, you enter a roblox username and it fetches the ID, however it only works once. Then gives the properties error.
const { Client, GatewayIntentBits } = require('discord.js')
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
});
const axios = require('axios');
const PREFIX = "!";


client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});


client.on('messageCreate', async (message) => {
if (message.content.startsWith(PREFIX)) {
const [command, username] = message.content
.substring(PREFIX.length)
.split(' ');

if (command === 'id') {
try {
// Clear the cache before making the API request
client.users.cache.clear();

const lowercaseUsername = username.toLowerCase();
const response = await fetch(`https://users.roblox.com/v1/users/search? keyword=${encodeURIComponent(lowercaseUsername)}`);
const { data } = await response.json();
if (data.length === 0) {
message.channel.send(`Could not find a user with the name "${username}".`);
} else {
const { id } = data[0];
console.log(`The user "${username}" has the Roblox ID ${id}.`);
message.channel.send(`The user "${username}" has the Roblox ID ${id}.`);
}
} catch (error) {
console.error(error);
message.channel.send(`There was an error searching for the user "${username}".`);
}
}
}
});
const { Client, GatewayIntentBits } = require('discord.js')
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
});
const axios = require('axios');
const PREFIX = "!";


client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});


client.on('messageCreate', async (message) => {
if (message.content.startsWith(PREFIX)) {
const [command, username] = message.content
.substring(PREFIX.length)
.split(' ');

if (command === 'id') {
try {
// Clear the cache before making the API request
client.users.cache.clear();

const lowercaseUsername = username.toLowerCase();
const response = await fetch(`https://users.roblox.com/v1/users/search? keyword=${encodeURIComponent(lowercaseUsername)}`);
const { data } = await response.json();
if (data.length === 0) {
message.channel.send(`Could not find a user with the name "${username}".`);
} else {
const { id } = data[0];
console.log(`The user "${username}" has the Roblox ID ${id}.`);
message.channel.send(`The user "${username}" has the Roblox ID ${id}.`);
}
} catch (error) {
console.error(error);
message.channel.send(`There was an error searching for the user "${username}".`);
}
}
}
});
5 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Zer0
Zer0OP2y ago
Ah, how can I delete this and I think it's data.length
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Zer0
Zer0OP2y ago
This thread
Want results from more Discord servers?
Add your server