Answer the question
In order to leave comments, you need to log in
TypeError: mongoose.connect is not a function?
when i use require commonjs everything works
const { Schema, connect } = require("mongoose");
connect("mongodb://localhost:27017/usersdb", {
useNewUrlParser: true
});
import { connect } from "mongoose";
connect(
"mongodb://localhost:27017/usersdb",
{
useNewUrlParser: true
}
);
import * as mongoose from "mongoose";
mongoose.connect("mongodb://localhost:27017/usersdb", {
useNewUrlParser: true
});
{
"name": "dur",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon -r esm src/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"esm": "^3.2.25",
"express": "^4.17.1",
"i": "^0.3.6",
"mongoose": "^5.7.8",
"nodemon": "^1.19.4"
}
}
Answer the question
In order to leave comments, you need to log in
I faced the same issue.
If you are using mongoose in a browser environment like me. I use react and in one of the components I tried to do mongoose.connect ()
In this case, the mongoose object will not have a connect method.
It is necessary to move the connection code into a separate file that will be executed in the nodejs environment.
Then mongoose will have the connect method and everything will work.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question