Generating static pages (3/4)BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer

Im getting this error when Im trying to build my app but when I run it on my pc everything is fine. what is the issue?

import clientPromise from "@/lib/mongodb";
import { ObjectId } from "mongodb";

export async function findIsomoById(itemId: string) {
  try {
    // Connect to the MongoDB database
    const client = await clientPromise;
    const db = client.db("amasomo_ya_misa");
    const collection = db.collection("amasomo");

   

    console.log(new ObjectId(itemId));

    // Find the item in the collection by its _id
    const isomo = await collection.findOne({ _id: new ObjectId(itemId) });

    return isomo;
  } catch (error) {
    console.error(error);
    throw new Error("An error occurred while finding the item.");
  } finally {
    //ServerClosedEvent.apply;
  }
}

[=   ] info  - Generating static pages (3/4)BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
    at new BSONTypeError (C:\Users\InezaGuy\reactpr\amasomo_yamisa\node_modules\bson\lib\error.js:41:28)
    at new ObjectId (C:\Users\InezaGuy\reactpr\amasomo_yamisa\node_modules\bson\lib\objectid.js:67:23)
    at findIsomoById (C:\Users\InezaGuy\reactpr\amasomo_yamisa\.next\server\app\isomo\[id]\page.js:553:21)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async AmasomoReview (C:\Users\InezaGuy\reactpr\amasomo_yamisa\.next\server\app\isomo\[id]\page.js:573:19)
Error: An error occurred while finding the item.
    at findIsomoById (C:\Users\InezaGuy\reactpr\amasomo_yamisa\.next\server\app\isomo\[id]\page.js:561:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async AmasomoReview (C:\Users\InezaGuy\reactpr\amasomo_yamisa\.next\server\app\isomo\[id]\page.js:573:19)

Error occurred prerendering page "/isomo/[id]". Read more: https://nextjs.org/docs/messages/prerender-error
Error: An error occurred while finding the item.
    at findIsomoById (C:\Users\InezaGuy\reactpr\amasomo_yamisa\.next\server\app\isomo\[id]\page.js:561:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async AmasomoReview (C:\Users\InezaGuy\reactpr\amasomo_yamisa\.next\server\app\isomo\[id]\page.js:573:19)
info  - Generating static pages (4/4)

Hi @Ineza_Guy - Welcome to the community.

What’s the value of the itemId variable being passed into ObjectId()? The error indicates that an invalid value is being passed through. Try logging the value of itemId prior to running the .find() for troubleshooting purposes. More information regarding ObjectId that may be of use.

mongosh example returning a similar error when using an invalid ObjectId value:

DB> ObjectId("63e58fb98a67c15905ee306c") /// <--- Valid ObjectId value
ObjectId("63e58fb98a67c15905ee306c")

DB> ObjectId("63e58fb98a67c15905ee306zzzzzzzzz") /// <--- Invalid ObjectId value
BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer

Regards,
Jason

so why is it working on npm run dev but on npm run build I get the errors?

1 Like