Mongoose connect is not a function... What is wrong?

I have tried to connect mongodb to REACT in a million different ways, using uri, putting the code straight on the ./app, doing the connection in another file and requiring it in the ./app page… nothing has worked.

I believe I have the username and password just fine, along with the database name…

Why am I keep getting these errors and not able to connect?

I have tried tons of different codes… this is just one of them…

const mongoose = require("mongoose");

const connection =

  "mongodb+srv://<myusernamehere>:<mypasswordhere>@cluster0.gcyyo.mongodb.net/<mydatabasehere>?retryWrites=true&w=majority";

mongoose

  .connect(connection, {

    useNewUrlParser: true,

    useUnifiedTopology: true,

    useFindAndModify: false,

  })

  .then(() => console.log("Database Connected Successfully"))

  .catch((err) => console.log(err));
1 Like

Did you get through? Im trying with Svelte 3.49 and Vite 3.1.0 using ES6 and I get the same error. Tried lots of “solutions”. None work. So frustrating how krappy web software is. I developed software for years on pc and most of the time it krapped out was human error. Web software is bug infested!

Has any solution been found for this issue?
I am having the same problem now.

Hello, everyone :wave:

It seems, you guys are trying to connect use Mongoose on the frontend.

From official Mongoose doc:

“Mongoose’s browser library is very limited. The only use case it supports is validating documents as shown below.”

That means it can not connect to a database, so no reason to use connect method on frontend.

In order to use connect method, mongoose object has to be initialized on the backend application and used on the server-side. If this method is missing in your node js app, then you have a dependency problem. Make sure you install, import, use your dependencies and run your application correctly.

Frontend app operates in browser and communicates with server, sending HTTP-requests to a server (backend app). The bankend then can use Mongoose to connect and do something with database and return result back to the frontend. Frontend does not interact with the database directly. At least, Mongoose is not the right tool for this.

1 Like

I solved this issue after reading your comment. I am using nextJS and I fixed it by going to the file where I have my mongoose.connect() function and on top of the file I imported ‘use server’ to make it a server component and it worked fine.