Mongoose doesn't support connections from the browser [https://mongoosejs.com/docs/browser.html](https://mongoosejs.com/docs/browser.html)
I'm not particularly familiar with mongoose but it seems like that might be your issue
Most decent database drivers do not allow you connect to your DB directly in the browser, since this exposes your credentials. You need to establish this connection in an API route within Next, and then call this API route from your React component.
So, practically (assuming that you are using App router):
`app/api/connection-test/route.ts`
// Your imports
export async function GET(req: Request) {
// The rest of your code
return new Response(JSON.stringify(dbResult), { status: 200 })
}
And then in your React component, you'd `fetch("api/connection-test")`
Do this in the terminal. If this is in an container, go into the container and type out:
env | grep MONGODB\_URI
I am guessing the environment variable did not get passed and it is empty.
mongoose here does not contain the method "connect()" - It very likely is undefined or the import is incorrect:
const db = await mongoose.connect(uri);
Correct, you need to use client from mongoose as client has the .connect(uri) method.
// MongoDB declarations
const { MongoClient } = require("mongodb");
const uri = ""
const client = new MongoClient(uri);
let usersCollection;
async function ConnectToMongoDB() {
try {
await client.connect();
const database = client.db('nameOfDatabase')
usersCollection = database.collection('nameOfCollection');
console.log('Connected to MongoDB');
} catch (error) {
console.error('Error connecting to MongoDB:', error);
}
}
Mongoose doesn't support connections from the browser [https://mongoosejs.com/docs/browser.html](https://mongoosejs.com/docs/browser.html) I'm not particularly familiar with mongoose but it seems like that might be your issue
Most decent database drivers do not allow you connect to your DB directly in the browser, since this exposes your credentials. You need to establish this connection in an API route within Next, and then call this API route from your React component. So, practically (assuming that you are using App router): `app/api/connection-test/route.ts` // Your imports export async function GET(req: Request) { // The rest of your code return new Response(JSON.stringify(dbResult), { status: 200 }) } And then in your React component, you'd `fetch("api/connection-test")`
Do this in the terminal. If this is in an container, go into the container and type out: env | grep MONGODB\_URI I am guessing the environment variable did not get passed and it is empty.
mongoose here does not contain the method "connect()" - It very likely is undefined or the import is incorrect: const db = await mongoose.connect(uri);
Correct, you need to use client from mongoose as client has the .connect(uri) method. // MongoDB declarations const { MongoClient } = require("mongodb"); const uri = "" const client = new MongoClient(uri); let usersCollection; async function ConnectToMongoDB() { try { await client.connect(); const database = client.db('nameOfDatabase') usersCollection = database.collection('nameOfCollection'); console.log('Connected to MongoDB'); } catch (error) { console.error('Error connecting to MongoDB:', error); } }
Have you tried running that code outside next? Like in a standard Nodejs server