Ticket Paging in M220js

I placed the following code in getMovies:

const displayCursor = cursor.skip(moviesPerPage*page)

I also tried to add to cursor = await movies the following:
.limit(20)

    .skip(page*20)

    page++

getting many errors. Have no idea how to proceed.

just add skip(page*20) followed by limit(20)

remove page++

2 Likes

Thanks, works like a charm. Is the page increase automatic in mongodb and this is why I do not need the page++ or a similar routine?
Best

skip(page*20) when you scroll down page increment happen and that many pages of 20 item would be skiped and remain upto to 20 item displays since limit is 20.

how exactly is the page variable is increasing is the code?
in the begining of the function page is set to 0 , and there is no operation on that variable during the code…
how is the scrolling down the page effect the page variable?
thanks!

The page variable is used as a parameter in the function, by default it will be assigned 0 value which will mean the first page.

That I believe a logic must have been written in the UI side, or api. Let me find it and share it with you. :slight_smile:

Kanika

I am not getting the expected output
my query is
const displayCursor = cursor.limit(moviesPerPage).skip(page * moviesPerPage)
why it is not working

Once you limit the documents the cursor has not enough documents to skip.

1 Like

I’ve added the following code to moviesDAO.js :
const displayCursor = cursor.skip(moviesPerPage*page).limit(moviesPerPage)

It passes two of the tests but its telling me that the total number of movies when I do this is 23530 instead of the expected 17903. Has anyone else recieved this error ?I have absolutely no idea what I’m doing wrong.
Thanks

The problem is not in the code you are showing. It lies where the total number of pages is calculated. Your query is not restrictive enough.

So we are calculating the total number of pages here:
const totalNumMovies = page === 0 ? await movies.countDocuments(query) : 0

The query is updated as the user scrolls down the page I think. I’m confused as to how to restrict the query. Or rather, should I restrict the movies variable that .find(query) is invoked on

edit: sorry, the query if I’m correct is the displayCursor variable. I know that it should only show 20 pages at a time, but I dont know how so many extra documents are being created. I’m trying to log some output to the screen but its not going so well

You should try it. It would make sense that you only want to count the documents that matches the query is used to show the movies.

Okay thanks, I’ll try this, and thanks for replying so quikcly :slight_smile:

I have added this to the code but it doesnt seem to be having any effect:

try {

  cursor = await movies
    .find(query).skip(moviesPerPage*page).limit(moviesPerPage)
    .project(project)
    .sort(sort)
    }

Am I going in the right direction here?

edit: Is the try block being run everytime the user scrolls down and the page variable is being updated?

I think I can see the route of the problem, the getMovies function is being run everytime new movies are being shown on the screen but the cursor var doesn’t have anything limiting it. I doesnt seem to matter whether I call the .limit on it or not though, the value of the total number of movies does not change.

That is still not the code that count the total number of movies that matches the query.

That’s the code that return the list of movies to display in the current page.

const totalNumMovies = page === 0 ? await movies.countDocuments(query) : 0

seems like the code that does this. Is that correct?

The import part is the query. What is the query?

I can see that an empty query is being passed to the movies.find() function and this is returing all of the films within that collection. I know what the sorting is doing but I dont know what the .project(project) is doing.

I Know that for this test, we are trying to filter by the arguments passed to the filter, “Comedy” and “Drama”. But I dont know how to create a general case. Is it only a simple modification required to facilitate this filtering or is there more requried. I’m currently thinking about passing the queryParams as an arguments to the movies.find() part of the get movies function

Edit: There was an error in my text and subfield search function that was carrying through to this ticket, sorry for the hassle :sweat_smile: