ShainLovesCoding
ShainLovesCoding

Reputation: 13

TypeError: Cannot read property 'members' of undefined. Discord bot Dashboard EJS issue

When I was busy coding on my Dashboard on my bot, I got this error for the dashboard link. The programming language was JS which was supposed to be compiled to EJS.

This is the code:

app.get("/dashboard", checkAuth, (req, res) => {
    const server = client.guilds.cache.get("903769597986103399");
    let user = server.members.cache.has(req.user.id);
});

This is the error: (from discord.js, I used Replit)

TypeError: Cannot read property 'members' of undefined
    at /home/runner/Kokili/dashboard/dashboard.js:640:25
    at Layer.handle [as handle_request] (/home/runner/Kokili/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/runner/Kokili/node_modules/express/lib/router/route.js:137:13)
    at checkAuth (/home/runner/Kokili/dashboard/dashboard.js:138:41)
    at Layer.handle [as handle_request] (/home/runner/Kokili/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/runner/Kokili/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/runner/Kokili/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/runner/Kokili/node_modules/express/lib/router/layer.js:95:5)
    at /home/runner/Kokili/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/runner/Kokili/node_modules/express/lib/router/index.js:335:12)
    at next (/home/runner/Kokili/node_modules/express/lib/router/index.js:275:10)
    at urlencodedParser (/home/runner/Kokili/node_modules/body-parser/lib/types/urlencoded.js:91:7)
    at Layer.handle [as handle_request] (/home/runner/Kokili/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/runner/Kokili/node_modules/express/lib/router/index.js:317:13)
    at /home/runner/Kokili/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/runner/Kokili/node_modules/express/lib/router/index.js:335:12)
    at next (/home/runner/Kokili/node_modules/express/lib/router/index.js:275:10)
    at jsonParser (/home/runner/Kokili/node_modules/body-parser/lib/types/json.js:110:7)
    at Layer.handle [as handle_request] (/home/runner/Kokili/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/runner/Kokili/node_modules/express/lib/router/index.js:317:13)
    at /home/runner/Kokili/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/runner/Kokili/node_modules/express/lib/router/index.js:335:12) ```

Upvotes: 1

Views: 72

Answers (1)

Hibiscus
Hibiscus

Reputation: 736

Caching

As pointed out by @Jakye, likely the guild you are trying to access has not been added to the cache. In order to resolve this, fetch the guild using <Guild>.fetch() then get the members. This would look something like this:

const server = client.guilds.cache.fetch("903769597986103399");

Upvotes: 1

Related Questions