FAIL test/migration.test.js

I am not able to pass the test of the Ticket: Migration. Even I have compared my codes(check below for details) with correct answer from course it everything looks okay but the test fails. May I know what might I am missing. Thanks.

const MongoClient = require("mongodb").MongoClient
const ObjectId = require("mongodb").ObjectId
const MongoError = require("mongodb").MongoError
require("dotenv").config()

// This syntax is called an Immediately Invoked Function Executioin (IIFE)
// It's useful for proper scoping, and in this case allowing us to use
// async/await syntax

;(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)

const predicate = { lastupdated: { $exists: true, $type: "string" } }
// we use the projection here to only return the _id and lastupdated fields
const projection = { lastupdated: 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: new Date(Date.parse(lastupdated)) },
    },
  },
}))
// What's the strange "\x1b[32m"? It's coloring. 31 is red, 32 is green
console.log(
  "\x1b[32m",
  `Found ${moviesToMigrate.length} documents to update`,
)
// Here's where we dispatch the bulk update. We destructure the
// modifiedCount key out of the result

const { modifiedCount } = await mflix
  .collection("movies")
  .bulkWrite(moviesToMigrate)

console.log("\x1b[32m", `${modifiedCount} documents updated`)
client.close()
process.exit(0)
  } catch (e) {
// check to see if the error was a MongoError and specifically a
// Invalid Operation error, meaning no documents to update
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)
  }
})()

What do you get when you run the migration?

Post a screenshot that show the results.

1 Like

I am getting that error.

Error during migration, Error: URL malformed, cannot be parsed

But, when I am setting .env as manual as below it is worked. May I know why process.env.MFLIX_DB_URI is undefined?

const host_undefiened = process.env.MFLIX_DB_URI;
    console.log(host_undefiened); //the output is undefined
    const host = 'mongodb+srv://m220student:m220password@mongodb-tutorial.mijqz.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'
    const client = await MongoClient.connect(host, { useNewUrlParser: true })
    const mflix = client.db('sample_mflix')