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
• 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.discord.js version: v14.11.0
node version: v18.15.0
can you explain in words what the error is?
or explain what the expected result is
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.
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
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•17mo ago
Message Not Public
Sign In & Join Server To View
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•17mo ago
Message Not Public
Sign In & Join Server To View
Lets not talk about that
Unknown User•17mo ago
Message Not Public
Sign In & Join Server To View
I have tried to solve the problem, but it didn't work. Could you please give me a hint on how to solve it?
make sure values are what you expect, and if theyre not, do some debugging to find out why
I did and I couldn't find the problem
Did you use a debugger?
Yes, it only shows me that:
Lookup a tutorial on how to use this debugger to step through your code line-by-line
Is there a debugger that you can recommend?
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)
Okay, I tried it but I didn't find any problem. Could you please help me? My code is here: https://srcb.in/kKZfvOCyyg
What did you try exactly? Step by step
I debugged it
Can you be more specific?
I read the article that you sent me, followed the steps they were saying there and didn't find any problems
Did you try stepping into your functions related to handling pagination?
What exactly do you mean with that?
Did you set any breakpoints?
Yes I did
Do you know how to step through code from a breakpoint?
Yes but when I did it didn't work anymore
The command failed?
The interaction
the button
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 responseYes, there is. You mean I should defer the interaction and then step through the code?
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
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
Did you write the underlying code?
what do you mean?
You know the code that handles pagination here has a bug. Did you write that code?
Of course
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
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
Break the calculation into steps until you can apply my expectation method
But not when the last item of a page is deleted and the previous page has to load
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
Okay, I'll try that
So I did - again - debug it and I just can't find the problem.
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
Okay, I have found the problem now, the buttonIndex is calculated wrong
If it changes to page 4 it gives me these buttonIndexes:
even tough they should be 7 and 8
Do you have any idea on how to fix this, because I don’t?
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?
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
(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)
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