Ticket - Migration: Error during test

Hello.
When I try to do the test I recive this error mesage:


C:\Users\pablo\Desktop\DAM2\mflix-js>npm test -t migration

server@1.0.0 test
jest --passWithNoTests “migration”

Determining test suites to run…Setup Mongo Connection
(node:3920) Warning: Accessing non-existent property ‘count’ of module exports inside circular dependency
(Use node --trace-warnings ... to show where the warning was created)
(node:3920) Warning: Accessing non-existent property ‘findOne’ of module exports inside circular dependency
(node:3920) Warning: Accessing non-existent property ‘remove’ of module exports inside circular dependency
(node:3920) Warning: Accessing non-existent property ‘updateOne’ of module exports inside circular dependency
(node:3920) 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/migration.test.js (30.969s)
Migration
× migration (48ms)

● Migration › migration

expect(received).not.toBeNull()

Received: null

  12 |       lastupdated: { $type: "date" },
  13 |     })
> 14 |     expect(movie).not.toBeNull()
     |                       ^
  15 |   })
  16 | })
  17 |

  at _callee2$ (test/migration.test.js:14:23)
  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)

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


The code that I have is :


const MongoClient = require(“mongodb”).MongoClient

const ObjectId = require(“mongodb”).ObjectId

const MongoError = require(“mongodb”).MongoError

require(“dotenv”).config()

/**

  • Ticket: Migration

  • Update all the documents in the movies collection, such that the

  • “lastupdated” field is stored as an ISODate() rather than a string.

  • The Date.parse() method build into Javascript will prove very useful here!

  • Refer to CRUD Operations

*/

// This leading semicolon (:wink: is to signify to the parser that this is a new expression. This expression is an

// Immediately Invoked Function Expression (IIFE). It’s being used to wrap this logic in an asynchronous function

// so we can use await within.

// To read more about this type of expression, refer to IIFE - MDN Web Docs Glossary: Definitions of Web-related terms | MDN

;(async () => {

try {

const host = process.env.MFLIX_DB_URI

const client = await MongoClient.connect(host, { useNewUrlParser: true })

const mflix = client.db(process.env.MFLIX_NS)

// TODO: Create the proper predicate and projection

// add a predicate that checks that the `lastupdated` field exists, and then

// check that its type is a string

// a projection is not required, but may help reduce the amount of data sent

// over the wire!

   

const predicate = { "lastupdated": { $exists: true } , "lastupdated": { $type: "string" } }

const projection = {"lastupdated": 1, _id:1}

const cursor = await mflix

  .collection("movies")

  .find(predicate, projection)

  .toArray()

const moviesToMigrate = cursor.map(({ _id, lastupdated }) => ({

  updateOne: {

    filter: { _id: ObjectId(_id) },

    update: {

      $set: { lastupdated: Date.parse(lastupdated) },

    },

  },

}))

console.log(

  "\x1b[32m",

  `Found ${moviesToMigrate.length} documents to update`,

)

// TODO: Complete the BulkWrite statement below

const { modifiedCount } = await mflix.collection("movies").bulkWrite([moviesToMigrate],{ordered: false})

console.log("\x1b[32m", `${modifiedCount} documents updated`)

client.close()

process.exit(0)

} catch (e) {

if (

  e instanceof MongoError &&

  e.message.slice(0, "Invalid Operation".length) === "Invalid Operation"

) {

  console.log("\x1b[32m", "No documents to update")

} else {

  console.error("\x1b[31m", `Error during migration, ${e}`)

}

process.exit(1)

}

})()


I don´t have idea why I´m receiving this error.
Any help will be well received.

Most likely it is because you either did not run the migration or the migration went wrong.

If you did run the migration, then the most likely reason would be you did not $set lastupdated to the right type. By looking closely at your code I see:

$set: { lastupdated: Date.parse(lastupdated) },

If you at the documentation of Date.parse you will see that it does not produce a Date but a number.

I was suspect of that code because I did not remember having to do anything on those line because the TODO was only to create the proper predicate and project. The rest of the code was given as is. So I went back and downloaded the .zip file again and I saw that the original code was doing the right thing. Just reset that line to the original and it should work once you reload the data set and rerun the migration.

You have to reload the data set because your migration changed lastupdated from string to number rather than string to Date.

I have to say that when I try to do the migration I receive this error:


C:\Users\pablo\Desktop\DAM2\mflix-js\src\migrations>node movie-last-updated-migration.js
(node:6416) Warning: Accessing non-existent property ‘count’ of module exports inside circular dependency
(Use node --trace-warnings ... to show where the warning was created)
(node:6416) Warning: Accessing non-existent property ‘findOne’ of module exports inside circular dependency
(node:6416) Warning: Accessing non-existent property ‘remove’ of module exports inside circular dependency
(node:6416) Warning: Accessing non-existent property ‘updateOne’ of module exports inside circular dependency
Error during migration, MongoParseError: URI malformed, cannot be parsed

C:\Users\pablo\Desktop\DAM2\mflix-js\src\migrations>


I don´t understand why this happend.

I posted it in other topic and they say me to try to cross-verify the connection String, and I don´t know how to do that. SO I tried to do the test without run the migration.


If that helps, my Uri is: mongodb+srv://m220student:m220password@mflix.1gz8b.mongodb.net/myFirstDatabase

Most likely because you ran the migration from within the migrations directory.

So the configuration could not be read and you ended up with an empty, thus malformed, URI.

I executed the migration code in the principal directory of the aplication (C:\Users\pablo\Desktop\DAM2\mflix-js>) and I receive this:


C:\Users\pablo\Desktop\DAM2\mflix-js>node movie-last-updated-migration.js
node:internal/modules/cjs/loader:936
throw err;
^

Error: Cannot find module ‘C:\Users\pablo\Desktop\DAM2\mflix-js\movie-last-updated-migration.js’
←[90m at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)←[39m
←[90m at Function.Module._load (node:internal/modules/cjs/loader:778:27)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)←[39m
←[90m at node:internal/main/run_main_module:17:47←[39m {
code: ←[32m’MODULE_NOT_FOUND’←[39m,
requireStack: []
}


You need to run in the top directory but the file is not located in the top directory.

You need to specify the path to the file.

Thank you, the migration runs properly now.

But I still receiving this:


FAIL test/migration.test.js
Migration
× migration (45ms)

● Migration › migration

expect(received).not.toBeNull()

Received: null

  12 |       lastupdated: { $type: "date" },
  13 |     })
> 14 |     expect(movie).not.toBeNull()
     |                       ^
  15 |   })
  16 | })
  17 |

  at _callee2$ (test/migration.test.js:14:23)
  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)

Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total


Any idea?

Share the output of running the migration.

C:\Users\pablo\Desktop\DAM2\mflix-js>node src\migrations\movie-last-updated-migration.js
(node:820) Warning: Accessing non-existent property ‘count’ of module exports inside circular dependency
(Use node --trace-warnings ... to show where the warning was created)
(node:820) Warning: Accessing non-existent property ‘findOne’ of module exports inside circular dependency
(node:820) Warning: Accessing non-existent property ‘remove’ of module exports inside circular dependency
(node:820) Warning: Accessing non-existent property ‘updateOne’ of module exports inside circular dependency
(node:820) 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.
Found 0 documents to update
No documents to update

C:\Users\pablo\Desktop\DAM2\mflix-js>

Since you

but the validation script find some documents that have lastupdated with the wrong type then the most likely cause is the one already mentioned

1 Like

Thank you very much.
Now all works.

1 Like