M220 - Ticket: Paging

Hello MongoDB Community,

I’m stuck at theTicket: Paging test in a weird situation: I have basically written the code to enable the infinite scrolling using .skip() and .limit() methods, it works on the as well as when I run the test command “npm test -t paging” (gives 3 green ticks and test suites says 1 passed), but when I go to the status page and run the checking it says “Paging: Did not receive the proper response when paging by text”.

I’m not sure where the bug is, given it works on the app and the npm test -t paging command responds positively.

My answer code (after the await movies.sort() method:

.skip(moviesPerPage * page)
.limit(moviesPerPage)

The result of the npm command:

Thank you in advance to whoever will reply. :slight_smile:

Have a great weekend!

And this is the result from the status page (It wouldn’t let me attach it to the post above):

Could you please share the whole function?

The following puzzles me:

1 Like

Hi Steevej,

Thanks for getting back to me.

I have managed to make it working, even if I’m not entirely sure how… (I was stuck on this test for a few hours, but it could have been a silly mistake like a forgotten _ or a non-camel cased variable).

Here’s the whole function:

static async getMovies({
    // here's where the default parameters are set for the getMovies method
    filters = null,
    page = 0,
    moviesPerPage = 20,
  } = {}) {
    let queryParams = {}
    if (filters) {
      if ("text" in filters) {
        queryParams = this.textSearchQuery(filters["text"])
      } else if ("cast" in filters) {
        queryParams = this.castSearchQuery(filters["cast"])
      } else if ("genre" in filters) {
        queryParams = this.genreSearchQuery(filters["genre"])
      }
    }

    let { query = {}, project = {}, sort = DEFAULT_SORT } = queryParams
    let cursor
    try {
      cursor = await movies
        .find(query)
        .project(project)
        .sort(sort)
    } catch (e) {
      console.error(`Unable to issue find command, ${e}`)
      return { moviesList: [], totalNumMovies: 0 }
    }

    /**
    Ticket: Paging

    Before this method returns back to the API, use the "moviesPerPage" and
    "page" arguments to decide the movies to display.

    Paging can be implemented by using the skip() and limit() cursor methods.
    */

    // TODO Ticket: Paging
    // Use the cursor to only return the movies that belong on the current page
    const displayCursor = cursor.skip(moviesPerPage * page).limit(moviesPerPage)

    try {
      const moviesList = await displayCursor.toArray()
      const totalNumMovies = page === 0 ? await movies.countDocuments(query) : 0

      return { moviesList, totalNumMovies }
    } catch (e) {
      console.error(
        `Unable to convert cursor to array or problem counting documents, ${e}`,
      )
      return { moviesList: [], totalNumMovies: 0 }
    }
  }

What I have done is move the skip() and limit() methods from the construction of the cursor variable to the construction of the displayCursor variable (as shown here).

Thanks again for coming back to me!

Alex

3 Likes