#Cannot read undefined 'name'

7 messages · Page 1 of 1 (latest)

unkempt rune
const model = mongoose.model('autoShard');
    let description = ``;

    try {
      const data = await model.find();

      if (data.length === 0) {
        return msg.reply('No active live shards');
      }

      for (const g of data) {
        const guild = msg.client.guilds.cache.get(g._id);

        if (!guild || !guild.name) continue;

        const owner = msg.client.users.cache.get(guild.ownerId);
        const channel = msg.client.channels.cache.get(g.channelId);
        
        console.log("Guilds:", guild.name);
        description += `**Guild:** ${guild?.name || "Unknown"} (${guild?.id || "Unknown"})\n**Owner:** ${owner?.username || "Unknown"} (${owner?.id || "Unknown"})\n**Channel:** ${channel.name} (${channel.id})\n\n`;
      }

So this code give the error somehow that name is undefined. I tried logging name for each docs, and mongo returns two documents and both two names are logged, then how is it giving an erro for undefined name? I can't wrap my heads around it... i placed additional checks to circumvent it like continue but still it gives that.

Also i should mention that on my test bot which i run locally, it works, but from my host, it gives this erro which i don't understand how the host would be a problem

grand flareBOT
  • 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!
  • Marked as resolved by OP
winter bay
unkempt rune
winter bay

That error isn't saying that name is undefined, but that whatever comes before .name is undefined

So if you're already continueing whenever guild is undefined, that must mean that channel is undefined

unkempt rune

Wait... i didn't think of that, all this time i was focused on guild name, let me see