Caching Issues
Hello! I'm fairly new to discord.js and I have some issued with this code I wrote.
I keep getting this error:
TypeError: Cannot read properties of undefined (reading 'cache')
33 Replies
- 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!what is oldMember and newMember?
Sorry for not specifying, this is a guildMemberUpdate event.
btw it doesn't really make sense to fetch your oldMember when you have a newMember fetched, fetching returns the current data
your current data is already your newMember
fetching won't return past data
So would I just remove the oldMember.fetch part and leave the newMember or remove both?
what do you want to do?
Let me remove both and get back to you.
It still says the same error.
that doesn't answer my question
Oh like what am I trying to do?
yeah
in general
what are you trying to achieve
I'm trying to check to see if a certain role got removed, and if so, it removes a different role.
I think you'd need to fetch all members beforehand
or at least the ones you want to track
the oldMember relies on cache
discord only emits the guildMemberUpdate event when the member changes, but it doesn't tell what changed or what was its old state
partials allow you to receive the event even without the member being cached, but there's pretty much no data you can get from that old state, since you don't have it
partial objects pretty much only have ids
ok ok
so would I fetch all members in the same file before everything or would i do it in lets say in a ready event
in ready
you can't do it on that event, at that point the member has already changed
okay makes sense
that's why this
that error is kinda weird though, I don't think it's coming from that codeblock
because GuildMember#roles is always present
It is there when I logged it
heres the full error
and which one is line 10 in 5050System.js
const oldRoles = oldMember.roles.cache.has(checkedRoleId);
maybe the underlying partial isn't being patched when you fetch
well regardless, you won't get that error anymore
because your approach will change entirely
okay ty
hey so uhm, im still getting the same error after fetching all the members in the guild
show your current code
and the one that errors
aka 5050System.js
sounds like a missmatch
wdym?
The order of function parameters must match between definition and function call.
- mismatch! you pass an interaction where the client is expected
- mismatch! you pass the client where an interaction is expected
let me log oldMember since thats the one thats erroring
in case you're appending/prepending a client to your event handlers, don't
every djs object has a client property
eg
oh
man i feel so stupid 😭
dw, it's a common thing that a lot of people encounter