M220JS Ticket: Projection failed

Hi! I’ve edited the code according to the instruction given on the ticket. Below is my code:

let cursor
try {
// TODO Ticket: Projection
// Find movies matching the “countries” list, but only return the title
// and _id. Do not put a limit in your own implementation, the limit
// here is only included to avoid sending 46000 documents down the
// wire.

  cursor = await movies.find({countries: {$in: countries}}).project({title: 1})

} catch (e) {
  console.error(`Unable to issue find command, ${e}`)
  return []
}
return cursor.toArray()

}


However, once I run the 

npm test -t projection

It gave me the following error:

> server@1.0.0 test
> jest --passWithNoTests "projection"

Determining test suites to run...Setup Mongo Connection
(node:12716) Warning: Accessing non-existent property 'count' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:12716) Warning: Accessing non-existent property 'findOne' of module exports inside circular dependency
(node:12716) Warning: Accessing non-existent property 'remove' of module exports inside circular dependency
(node:12716) Warning: Accessing non-existent property 'updateOne' of module exports inside circular dependency
(node:12716) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to MongoClient.connect.
 FAIL  test/projection.test.js
  Projection
    × Can perform a country search for one country (147ms)
    × Can perform a country search for three countries (54ms)

  ● Projection › Can perform a country search for one country

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

    Expected: 2
    Received: 1

       9 |     const kosovoList = ["Kosovo"]
      10 |     const movies = await MoviesDAO.getMoviesByCountry(kosovoList)
    > 11 |     expect(movies.length).toEqual(2)
         |                           ^
      12 |   })
      13 | 
      14 |   test("Can perform a country search for three countries", async () => {

      at toEqual (test/projection.test.js:11:27)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
      at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:271:22)
      at Generator.prototype.<computed> [as next] (node_modules/regenerator-runtime/runtime.js:97:21)
      at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
      at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

  ● Projection › Can perform a country search for three countries

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

    Expected: 1468
    Received: 1

      15 |     const countriesList = ["Russia", "Japan", "Mexico"]
      16 |     const movies = await MoviesDAO.getMoviesByCountry(countriesList)
    > 17 |     expect(movies.length).toEqual(1468)
         |                           ^
      18 |     movies.map(movie => {
      19 |       const movieKeys = Object.keys(movie).sort()
      20 |       const expectedKeys = ["_id", "title"]

      at toEqual (test/projection.test.js:17:27)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
      at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:271:22)
      at Generator.prototype.<computed> [as next] (node_modules/regenerator-runtime/runtime.js:97:21)
      at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
      at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

Test Suites: 1 failed, 1 total
Tests:       2 failed, 2 total
Snapshots:   0 total
Time:        9.93s
Ran all test suites matching /projection/i.
Teardown Mongo Connection

I am very new to Javascript and coding. Can anyone please help me/guide me on what to do?

Hi Najwa_Najihah:

Here’s an example in the basic-reads.spec.js.

    // Note that only the fields we specify in the projection section of the
    // query will be returned in our result set with the exception of the _id
    // field.We need to explicitly specify when we don't want the _id field to
    // be returned. Lets try that.

    let result2 = await movies.findOne(
      { cast: filter },
      { projection: { title: 1, year: 1, _id: 0 } },
    )

    expect(result2).not.toBeNull()
    expect(Object.keys(result2).length).toBe(2)

    console.log(result2)

Hope this helps.

Thank you for your reply. I tried out according to the code from basic-reads.spec.js. and still got the same error when I ran npm test -t projection. Do we need to define the countries? Example:

let countries = [ Kosovo, Russia, Japan, Mexico ] ?

Which “countries” do you mean?
I guess it would only make sense if you meant the arguement passed into this function.
So we do not have define it since it is already an arguement.

Do not use findOne as shared by jaden_hsu because they expect a list of movies.

You shared the code without the limit(1) but you always return only 1 movie. Perhaps you ran the test before removing it.

this is the code that I ran according to what has been shared by jaden_hsu & the error is still the same.

let cursor

    try {

      cursor = await movies.find( { countries: { $in: countries } },

        { projection: {title: 1 } }, )

    } catch (e) {

      console.error(`Unable to issue find command, ${e}`)

      return []

    }

    return cursor.toArray()

  }

and I still return only 1 movies for both tests.

Share the whole function.

/**

  • Finds and returns movies originating from one or more countries.

  • Returns a list of objects, each object contains a title and an _id.

  • @param {string} countries - The list of countries.

  • @returns {Promise} A promise that will resolve to a list of CountryResults.

*/

static async getMoviesByCountry(countries) {

/**

Ticket: Projection

Write a query that matches movies with the countries in the "countries"

list, but only returns the title and _id of each movie.

Remember that in MongoDB, the $in operator can be used with a list to

match one or more values of a specific field.

*/



let cursor

try {

  cursor = await movies.find( { countries: { $in: countries } },

    { projection: {title: 1 } }, )

} catch (e) {

  console.error(`Unable to issue find command, ${e}`)

  return []

}

return cursor.toArray()

}

server@1.0.0 test
jest --passWithNoTests “projection”

Determining test suites to run…Setup Mongo Connection
(node:13008) Warning: Accessing non-existent property ‘count’ of module exports inside circular dependency
(Use node --trace-warnings ... to show where the warning was created)
(node:13008) Warning: Accessing non-existent property ‘findOne’ of module exports inside circular dependency
(node:13008) Warning: Accessing non-existent property ‘remove’ of module exports inside circular dependency
(node:13008) Warning: Accessing non-existent property ‘updateOne’ of module exports inside circular dependency
(node:13008) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to MongoClient.connect.
FAIL test/projection.test.js
Projection
× Can perform a country search for one country (117ms)
× Can perform a country search for three countries (50ms)

● Projection › Can perform a country search for one country

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

Expected: 2
Received: 1

   9 |     const kosovoList = ["Kosovo"]
  10 |     const movies = await MoviesDAO.getMoviesByCountry(kosovoList)
> 11 |     expect(movies.length).toEqual(2)
     |                           ^
  12 |   })
  13 | 
  14 |   test("Can perform a country search for three countries", async () => {

  at toEqual (test/projection.test.js:11:27)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:271:22)
  at Generator.prototype.<computed> [as next] (node_modules/regenerator-runtime/runtime.js:97:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
  at processTicksAndRejections (node:internal/process/task_queues:96:5)

● Projection › Can perform a country search for three countries

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

Expected: 1468
Received: 1

  15 |     const countriesList = ["Russia", "Japan", "Mexico"]
  16 |     const movies = await MoviesDAO.getMoviesByCountry(countriesList)
> 17 |     expect(movies.length).toEqual(1468)
     |                           ^
  18 |     movies.map(movie => {
  19 |       const movieKeys = Object.keys(movie).sort()
  20 |       const expectedKeys = ["_id", "title"]

  at toEqual (test/projection.test.js:17:27)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:271:22)
  at Generator.prototype.<computed> [as next] (node_modules/regenerator-runtime/runtime.js:97:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
  at processTicksAndRejections (node:internal/process/task_queues:96:5)

Test Suites: 1 failed, 1 total
Tests: 2 failed, 2 total
Snapshots: 0 total
Time: 5.961s
Ran all test suites matching /projection/i.
Teardown Mongo Connection

because when I tried running the query in MongoDB compass, I can returned the exact amount of docs:

so I thought maybe I need to define the parameter countries. However, that didn’t work too. So, now I don’t know how to solve for the problem. :sweat_smile:

Do you have any other suggestion?

Are you sure you connect to the same database?

Try removing the projection to see if it fails elsewhere.

1 Like


just try this

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