Ticket: Migration [Chapter 3] (Cannot find module)

After I run “node movie-last-updated-migration.js” in my CLI, I got an error:

internal/modules/cjs/loader.js:584
throw err;
^

Error: Cannot find module ‘C:\Users\LC\mflix\movie-last-updated-migration.js’
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
at Function.Module._load (internal/modules/cjs/loader.js:508:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Why was I getting this error? How can I solve it?

Hi @Cong_09328,

Please try from the directory src/migrations or providing the full path like:

 mflix-js> node src/migrations/movie-last-updated-migration.js 

Kanika

2 Likes

Thanks, Kanika.
Now I can successfully run that command.

I’ve run “node movie-last-updated-migration.js” in my CLI, and I have been waiting for hours, there is nothing on my screen, what should I do now?

1 Like

Here is my code:

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

;(async () => {
  try {
    const host = "mongodb+srv://m220student: ***password@mflix-m4v11.mongodb.net/test"
    const client = await MongoClient.connect(
      host,
      { useNewUrlParser: true },
    )
    const mflix = client.db("mflix")

    const predicate = { lastupdated: { $type: "string" } }
    //copied from discussion
    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(lastupdated) },
        },
      },
    }));
    console.log(
      "\x1b[32m",
      `Found ${moviesToMigrate.length} documents to update`,
    )

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

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

Hi @Cong_09328,

I assume that you have used your password here.

You might want to parse the string to a particular format before converting it into a Date object. Try the below code:

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

Let me know if it works!

Kanika

Acutally, all my problems are gone after I deleted all the strange strings, like “\x1b[32m”.