AwesomeDude091
AwesomeDude091

Reputation: 107

Cannot read property fetch of undefined

My friend originally said this as a joke, but he told me to make my own discord bot, so here I am weeks later actually doing it because I found fun in it. So whenever I run this it just gives me the error that it," cannot read the property fetch of undefined" The Bot is operational with other commands, this is meant as an expansion for future functions. Additionally it is hosted off of a raspberry pi with nodemon --inspect

Here is my code:

const guild = client.guilds.fetch("xxxxxxxxxxxxxx");
var memberList = [];
var randomInt = new Number(0);
var User = {
    id: 0,
    FollowUp: 0,
    name: "Tes",
    statement: ""
};
var Person = {};

function myFunction(value) {
    const id = new Number(0);
    id = value.id;
    Person[id] = new User();
    Person[id].id = value.id;
    Person[id].name = value.user.username;
    Person[id].statement = "";
    Person[id].FollowUp = new Number(0);
}

client.on('ready', () => {
    console.log('Ready!');
    memberList = guild.members.fetch();
    memberList.forEach(myFunction);
});

Here are the error logs:

(node:2204) UnhandledPromiseRejectionWarning: TypeError: Object.entries(...).filter(...).flatMap is not a function at new APIRequest (/home/user/node_modules/discord.js/src/rest/APIRequest.js:24:10) at RESTManager.request (/home/user/node_modules/discord.js/src/rest/RESTManager.js:39:24) at Proxy.options (/home/user/node_modules/discord.js/src/rest/APIRouter.js:30:19) at GuildManager.fetch (/home/user/node_modules/discord.js/src/managers/GuildManager.js:247:51) at Object. (/home/user/Downloads/Bot/index.js:7:29) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) (node:2204) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:2204) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. Ready! (node:2204) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'fetch' of undefined at Client.client.on (/home/user/Downloads/Bot/index.js:31:32) at Client.emit (events.js:198:13) at WebSocketManager.triggerClientReady (/home/user/node_modules/discord.js/src/client/websocket/WebSocketManager.js:431:17) at WebSocketManager.checkShardsReady (/home/user/node_modules/discord.js/src/client/websocket/WebSocketManager.js:415:10) at WebSocketShard.shard.on.unavailableGuilds (/home/user/node_modules/discord.js/src/client/websocket/WebSocketManager.js:197:14) at WebSocketShard.emit (events.js:198:13) at WebSocketShard.checkReady (/home/user/node_modules/discord.js/src/client/websocket/WebSocketShard.js:475:12) at WebSocketShard.onPacket (/home/user/node_modules/discord.js/src/client/websocket/WebSocketShard.js:447:16) at WebSocketShard.onMessage (/home/user/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10) at WebSocket.onMessage (/home/user/node_modules/ws/lib/event-target.js:132:16) (node:2204) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

I can't find anything with google, so I hope that you guys can help me, Cheers!

Upvotes: 0

Views: 1126

Answers (1)

Yankue
Yankue

Reputation: 378

As the error suggests, it can't read the property fetch of undefined, meaning the guild is undefined. I can spot 2 issues:

  1. Client can't be accessed before it's ready. Move your const guild = client.guilds.fetch("xxxxxxxxxxxxxx"); inside the client.on("ready" listener.
  1. Replace client.guilds.fetch("xxxxxxxxxxxxxx"); with client.guilds.cache.get("xxxxxxxxxxxxxx");, using .get() on a cache collection. I hope that helped!

Upvotes: 0

Related Questions