Pagination error

Hey, I have a problem with my pagination. I'll attach a video so you can understand the problem. Here's my code: https://sourceb.in/k6HUMLoMJ2
53 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
EndergamerMC
EndergamerMCOP2y ago
discord.js version: v14.11.0 node version: v18.15.0
grass
grass2y ago
can you explain in words what the error is? or explain what the expected result is
EndergamerMC
EndergamerMCOP2y ago
sure I have 5 objects from a database in one embed (page size = 5) so that only 5 objects show on one page of the embed. underneath I have buttons to do certain actions with the objects (move them to a different list of objects in the database) and then two buttons to cycle through the pages. When I move the last item of a page to a different list of objects then the indexing doesn't work correctly anymore.
Auride
Auride2y ago
That sounds like a logical error with your pagination code, rather than a discord js issue I would go through the code that updates those pages line-by-line and think about what you expect to happen vs what will actually happen
EndergamerMC
EndergamerMCOP2y ago
That’s the problem, i looked through my code over and over again and I just can’t find why it is not working correctly
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Auride
Auride2y ago
Have you tried using breakpoints and a debugger to actually see where the data stops matching your expectations? Might have to mock up the test case to avoid timeouts from discord
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EndergamerMC
EndergamerMCOP2y ago
Lets not talk about that
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EndergamerMC
EndergamerMCOP2y ago
I have tried to solve the problem, but it didn't work. Could you please give me a hint on how to solve it?
grass
grass2y ago
make sure values are what you expect, and if theyre not, do some debugging to find out why
EndergamerMC
EndergamerMCOP2y ago
I did and I couldn't find the problem
Auride
Auride2y ago
Did you use a debugger?
EndergamerMC
EndergamerMCOP2y ago
Yes, it only shows me that:
EndergamerMC
EndergamerMCOP2y ago
Auride
Auride2y ago
Lookup a tutorial on how to use this debugger to step through your code line-by-line
EndergamerMC
EndergamerMCOP2y ago
Is there a debugger that you can recommend?
Auride
Auride2y ago
You can use the Google chrome dev tools. Follow this blog post https://medium.com/@paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27
Medium
Debugging Node.js with Chrome DevTools
Support for Node.js debuggability landed in Node.js in 2016. Here’s how to get up and running. (Post updated Jan 2018)
EndergamerMC
EndergamerMCOP2y ago
Okay, I tried it but I didn't find any problem. Could you please help me? My code is here: https://srcb.in/kKZfvOCyyg
Auride
Auride2y ago
What did you try exactly? Step by step
EndergamerMC
EndergamerMCOP2y ago
I debugged it
Auride
Auride2y ago
Can you be more specific?
EndergamerMC
EndergamerMCOP2y ago
I read the article that you sent me, followed the steps they were saying there and didn't find any problems
Auride
Auride2y ago
Did you try stepping into your functions related to handling pagination?
EndergamerMC
EndergamerMCOP2y ago
What exactly do you mean with that?
Auride
Auride2y ago
Did you set any breakpoints?
EndergamerMC
EndergamerMCOP2y ago
Yes I did
Auride
Auride2y ago
Do you know how to step through code from a breakpoint?
EndergamerMC
EndergamerMCOP2y ago
Yes but when I did it didn't work anymore
Auride
Auride2y ago
The command failed?
EndergamerMC
EndergamerMCOP2y ago
The interaction the button
Auride
Auride2y ago
That makes sense, since the breakpoint prevents the bot from responding within the time limit Is there a defer method for button interactions? That could buy you some time to debug and still see the resulting response
EndergamerMC
EndergamerMCOP2y ago
Yes, there is. You mean I should defer the interaction and then step through the code?
Auride
Auride2y ago
Yeah, just so you can keep trying and seeing what the result is Keep in mind you can run code in the debugger console while stepping through, to try modifying values Without having to make changes to the source files and restarting the process
EndergamerMC
EndergamerMCOP2y ago
So It may sound a bit stupid, but I have tried everything possible, looked through the code multiple times and I just can't find the solution to the problem. Even after debugging
Auride
Auride2y ago
Did you write the underlying code?
EndergamerMC
EndergamerMCOP2y ago
what do you mean?
Auride
Auride2y ago
You know the code that handles pagination here has a bug. Did you write that code?
EndergamerMC
EndergamerMCOP2y ago
Of course
Auride
Auride2y ago
If you are capable of writing it, and you know there's a bug in it, you are definitely capable of finding the bug. I don't know what you saw when debugging, but for me, I always find places in my code where I expect certain variables to hold certain values, and then I check if my expectations are correct Using the debugger Either I find a place where my expectations are wrong, which usually leads to the bug, or I don't find anything and I can eliminate those expectations as causes of the bug And then develop new expectations to test
EndergamerMC
EndergamerMCOP2y ago
The problem is, that I just don't know why it is calculating the values wrong. It should work fine And it works every other time
Auride
Auride2y ago
Break the calculation into steps until you can apply my expectation method
EndergamerMC
EndergamerMCOP2y ago
But not when the last item of a page is deleted and the previous page has to load
Auride
Auride2y ago
Debug that specific case. Follow from inputs until something unexpected happens I have a tip that might help: try coming up with a simpler test case. For example, try modifying the code so that there are only 1 or 2 items per page rather than 5, then see if the bug still happens That will make the problem easier to reason about without getting overwhelmed
EndergamerMC
EndergamerMCOP2y ago
Okay, I'll try that So I did - again - debug it and I just can't find the problem.
Auride
Auride2y ago
const buttonIndex = startIndex + index + 1;
const buttonIndex = startIndex + index + 1;
set a breakpoint on this line in the demonstrated case, what do you expect each of these values to be the first time this line gets executed? then compare with what they actually are
EndergamerMC
EndergamerMCOP2y ago
Okay, I have found the problem now, the buttonIndex is calculated wrong If it changes to page 4 it gives me these buttonIndexes:
buttonIndex: 9
buttonIndex: 10
buttonIndex: 9
buttonIndex: 10
even tough they should be 7 and 8 Do you have any idea on how to fix this, because I don’t?
souji
souji2y ago
so you have 9 and 10 (note the difference of +1) and want to translate it to 7 and 8 (note the difference of +1) to fix this: apply math! (in essence, this is an index shift) how do you get from 9 to 7?
EndergamerMC
EndergamerMCOP2y ago
By doing -2 but when I do this, then the index for the buttons when it’s not the last item on a page gets calculated correctly Or am I wrong
souji
souji2y ago
(well, since you are doing +1 i'd argue that removing that and doing -1 instead is likely a better approach, which is trivially equivalent to -2)
Auride
Auride2y ago
Once you find a place where your expectations don't match reality, you should go backwards through the code and find where those variables are modified
Want results from more Discord servers?
Add your server