MG91
MG91

Reputation: 193

unable to add a role to a member property 'roles' of undefined

I am trying to grant users a role once they submit a form but I am getting this in nodejs

TypeError: Cannot read property 'roles' of undefined

I have the code set up like this:

const guild = discordClient.guilds.cache.get('1234567890');
const member = await guild.members.cache.get('22222222')
const role = await guild.roles.cache.find(role => role.id === '111111');
member.roles.add(role);

what am I doing wrong?

Upvotes: 2

Views: 94

Answers (2)

SP73
SP73

Reputation: 415

Please refer to the docs before asking questions. Anyways the answer would be

let guild = client.guilds.cache.fetch('id');
const role = await guild.roles.cache.find(role => role.id === 'ROLEID');
const member = await guild.members.fetch(req.user.discord_id);
member.roles.add(role);

as seen here on the docs

Upvotes: 1

SatvikVejendla
SatvikVejendla

Reputation: 459

Try using this declaration for the guild.

const guild = message.guild;

Upvotes: 2

Related Questions