Failed to fetch error in @discordjs/rest

Hi, I'm using @discordjs/rest & discord-api-types, I'm following the documentation, but for some reason, I'm getting Failed to fetch error.
try {
// Get the guild member data to ensure the user is part of the guild
const memberData = await rest.get(Routes.guildMember(guildId, userId));
console.log(memberData);

if (memberData) {
// Add the role to the user
await rest.put(Routes.guildMemberRole(guildId, userId, roleId));

console.log(`Successfully added role to user with ID: ${userId}`);
} else {
console.error(`User with ID ${userId} is not a member of the guild.`);
}
} catch (error) {
console.error(error);
}
try {
// Get the guild member data to ensure the user is part of the guild
const memberData = await rest.get(Routes.guildMember(guildId, userId));
console.log(memberData);

if (memberData) {
// Add the role to the user
await rest.put(Routes.guildMemberRole(guildId, userId, roleId));

console.log(`Successfully added role to user with ID: ${userId}`);
} else {
console.error(`User with ID ${userId} is not a member of the guild.`);
}
} catch (error) {
console.error(error);
}
31 Replies
d.js toolkit
d.js toolkitβ€’12mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - 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 staff
InFiNiTy
InFiNiTyβ€’12mo ago
So would you give error with stacktrace to better understand the issue?
Farhan
FarhanOPβ€’12mo ago
No description
InFiNiTy
InFiNiTyβ€’12mo ago
hmm does memberData return anything?
Farhan
FarhanOPβ€’12mo ago
none, actually the error is coming from memberData
InFiNiTy
InFiNiTyβ€’12mo ago
maybe try to put in rest options makeRequest: fetch
treble/luna
treble/lunaβ€’12mo ago
Make sure both memberId and guildId are valid snowflakes
Farhan
FarhanOPβ€’12mo ago
yeah, I checked it. seems to be both Ids are valid
Farhan
FarhanOPβ€’12mo ago
No description
Farhan
FarhanOPβ€’12mo ago
here is my full code can you elaborate more?
InFiNiTy
InFiNiTyβ€’12mo ago
in line 10 after key option in new REST call, so version: '10' put another option makeRequest: fetch
InFiNiTy
InFiNiTyβ€’12mo ago
by the way why the heck you either import and require packages?
No description
Farhan
FarhanOPβ€’12mo ago
my godddd, damn. I didn't notice monkaS
InFiNiTy
InFiNiTyβ€’12mo ago
np just curious πŸ˜…
Farhan
FarhanOPβ€’12mo ago
but also getting the same error tho πŸ™‚
InFiNiTy
InFiNiTyβ€’12mo ago
yeah that's not the issue, just curious so makeRequest did not help?
Farhan
FarhanOPβ€’12mo ago
It requires 2 params
No description
InFiNiTy
InFiNiTyβ€’12mo ago
bru not the function call 'fetch' mb
Farhan
FarhanOPβ€’12mo ago
yup, I tried before. but a type err throwing: Type 'string' is not assignable to type '(url: string, init: RequestInit) => Promise<ResponseLike>'.
InFiNiTy
InFiNiTyβ€’12mo ago
huh weird even docs say that
Farhan
FarhanOPβ€’12mo ago
you can see
No description
InFiNiTy
InFiNiTyβ€’12mo ago
huh
No description
InFiNiTy
InFiNiTyβ€’12mo ago
huh v2
No description
treble/luna
treble/lunaβ€’12mo ago
maybe send those docs instead of just cropped screenshots then
InFiNiTy
InFiNiTyβ€’12mo ago
I've just sent those screenshots from link you've provided https://discord.js.org/docs/packages/rest/main Section Send a basic message in an edge environment
treble/luna
treble/lunaβ€’12mo ago
well yeah, thats fetch not 'fetch'
Farhan
FarhanOPβ€’12mo ago
Then why this error showing to me?
No description
treble/luna
treble/lunaβ€’12mo ago
pass it a proper fetch node:fetch or unidici, or whichever you prefer worked with undici for me
Farhan
FarhanOPβ€’12mo ago
Okay, let me check. I'm unable to use undici, coz Next Js is not allowing me to. there are several errors, and node-fetch doesn't work either actually it's now working, I just had to take this function to a server component, and send the request through dogeHaHa ❄ 🌈 @InFiNiTy Thank you guys for your time & help❀️
InFiNiTy
InFiNiTyβ€’12mo ago
D:
Want results from more Discord servers?
Add your server