mongoose.connect is not a function?

Hi, When I try to connect to my MongoDB database using the mongoose.connect function, I get the error message that this function doesn't exist, even though it does. I've tried various methods to import Mongoose, but none work. Here's the code:

 import { connect } from 'mongoose' import { userModel } from './models/User' const auth = await connect("connect-string") export async function createUser(username:String, password:String) { try { const result = await userModel.create({ username: username, password: password }) console.log(result) } catch (error) { console.log(error) } }

"connect-string" is only changed here

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
1 Answer
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
regex9
1 year ago

You cannot connect to the database within the browser context. Therefore there is no support for connect– Function.

Run your database actions on the server side. If they are to be launched in the frontend (Browser), then use AJAX requests that provide the information to a corresponding request handler on the server side.