Ticket: Paging - ✕ Supports paging by genre - failing test

/mflix-js/src/dao/moviesDAO.js - line: 262
// TODO Ticket: Paging
// Use the cursor to only return the movies that belong on the current page
const displayCursor = cursor.limit(moviesPerPage).skip(page * moviesPerPage)

I added the skip method with (page * moviesPerPage), so it will be calculated with the params. the ‘cast’ and ‘text’ filters tests successfully passed, so I guess the problem is not at the code implement (let me know if I’m wrong).

when I get into the paging.test.js, I found that the test search for ‘Comedy’ and ‘Drama’ genres, maybe the sample data has been changed? also tried to delete all the sample data and to download it again - still got the same error. please help me with that.

console output >>>

Paging
✓ Supports paging by cast (1735ms)
✕ Supports paging by genre (641ms)
✓ Supports paging by text (744ms)

● Paging › Supports paging by genre

expect(received).toEqual(expected) // deep equality

Expected: 17903
Received: 23530

Try with skip and limit reversed :slight_smile:

Kanika

const displayCursor = cursor.skip(page * moviesPerPage).limit(moviesPerPage)
still the same issue

If the other two are working, I then suspect the genres filter to be not restrictive enough. If you do not mind sharing we can take a look.

@steevej-1495 - the thing is that this is the first task in the course that asked to change something in the code. so I even didn’t touch this part of the function. adding the all function code -

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.limit(moviesPerPage).skip(page * 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 }
}

}

@kanikasingla - maybe the same problem that this post had?

Looks like you know the solution :slight_smile: Can you backup your files and try with the latest handout please?

Kanika

just downloaded the latest handout - still the spec file (test) expected 17903 results. so I believe it’s something with the sample dataset (I also delete all the collections and download the dataset again - with no change)

is someone related to the university and can help? am I doing something wrong or it’s something with the DB documents?

solved - by getting back to the previous chapter and implementing the genre filter function.