M220JS: Chapter 2 -- paging

I guess I’m reading the instructions wrong I added added the skip() and limit() methods and used the page and moviesPerPage variables aa instructed but it fails the test

const displayCursor = cursor.limit(moviesPerPage).skip(page)

I’ve not done the 220JS course in a very long time (not a developer :frowning:) but wouldn’t you want to skip page * moviesPerPage? The way you have things now you’re going to grab documents 1 to x on the first page, 2 to x+1 on the next page, etc.

1 Like

Hi Doug

I’m did that based on the video and documentation on the file cursor-methods-agg-equivalents.spec.js under test/lesons directory

In the moviesDAO.js file, the getMovies() method,
When the page value is 1,
const totalNumMovies = page === 0 ? await movies.countDocuments(query) : 0 returns totalNumMovies = 0.
This should be the 20 correct?

page * moviesPerPage

This worked for the first 2 tests. Please disregard the previous question

1 Like

Hi @Chris_Job1

when you use both “skip” and “limit”, if you don’t have evil plans then you would never use the “limit” before the “skip”.

let’s say I have 100 documents from the match, if I say “limit 10” then it reduces the total to 10, and then after I say “skip 20” there will be no more documents left to retrieve. but if you reverse the order, then the result set will be the 21th to 30th documents out of those 100.

The order of skip and limit is irrelevant when working with cursors.

I could not find back and documentation to confirm the above but I remember seeing it documented.

2 Likes

Cursors should .sort() then .skip() and finally .limit() no matter the order they are placed on the query.

3 Likes

Thanks for the better wording. It is much clearer.

Thanks you all for helping to clarifying that for me!!

Now I’m passing the tests but failing in the application on genre (see attached)

In the paging.test.js file,
const filters = { genre: [“Comedy”, “Drama”] }
fails.

if I make it const filters = { genres: [“Comedy”, “Drama”] }
the tests pass

I noticed that the test is using genre for the field name and the database has the name: genres


I got it to work!!
I had to make the field name genres in the movies.controller.js file

switch (searchType) {
  case "genre":
    if (req.query.genre !== "") {
      filters.genres = req.query.genre
      console.log(filters);
    }

try to revert that change and instead implement that TODO in the “moviesDAO.js”.

In this course, you are expected to implement changes mainly in the file in “dao” folder, as the main idea is to learn how to connect&query the database.

as for the TODOs, most of the solutions are one-liners if they are cursors, or are a few lines if they are data objects. so keep this in mind, for the rest of the course: keep it simple.

by the way, I had forgotten that property of cursor methods in my above post. Yet it is, in my opinion, a good practice to keep the order of operations as occurred in natural language: sort these documents, skip some of them, and get only that many to be db.coll.find().sort().skip().limit(). this also helps to switch between cursors and aggregates without confusion.

3 Likes

I figured you would tell I making it more complicated than it should be!!
Thanks, I’ll roll back the changes and work within the DAO file

1 Like

:laughing:

We all have a tendency to over-engineer at times. Take a deep breath before starting the assignment, read through the text, take another deep breath and reread the text. Then simplify your thoughts. :wink:

Best of luck on passing the course!

2 Likes

Thanks for the words of advice.
I rolled back the changes and re-read the instructions

I added the query for the genre and it passes the test BUT it keeps failing in the web app. That’s what led me down the rabbit hole of changing the other files :face_with_spiral_eyes:

// TODO Ticket: Text and Subfield Search
// Construct a query that will search for the chosen genre.
const query = {genres:{$in: searchGenre}}
const project = {}
const sort = DEFAULT_SORT

TL:DR Refresh the page and retry validating.

I have used my already-complete mflix app and I got the similar error for paging validation for text search. I was lucky to get a similar error on the page. I said “luck” because it should not happen. To be honest, I don’t know under what conditions that happens, but regardless it happens.

The thing is that if you are getting validation correctly in the console, then the error in the web page might be an incident that might never happen again. Just refresh the page and retry validation. The error you get should just go away as if nothing had happened.

I rebooted my machine and the web app validation worked!!
Thanks you for getting out of that rabbit hole!! :grinning:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.