I am attempting to connect to MongoDB via the package mongoose
, but get an error along the lines of MongoError: database name must be a string
.
I am using Windows, within the script I am also connecting to 2 other APIs which are both connected. I have tried adding my database name when requiring mongoose and also when connecting (.MyDatabaseName
to the end).
Mongoose.connect("mongodb+srv://MyUserName:[email protected]", {useNewUrlParser: true}).EternalsMilitary;
Mongoose.connect.once("open", function() {
console.log("Connected To MongoDB");
}).on("error", function(err) {
console.log("Error Connecting To MongoDB: ", err);
});
It's expected to output connected
, but it errors with MongoError: database name must be a string
.
I am attempting to connect to MongoDB via the package mongoose
, but get an error along the lines of MongoError: database name must be a string
.
I am using Windows, within the script I am also connecting to 2 other APIs which are both connected. I have tried adding my database name when requiring mongoose and also when connecting (.MyDatabaseName
to the end).
Mongoose.connect("mongodb+srv://MyUserName:[email protected]", {useNewUrlParser: true}).EternalsMilitary;
Mongoose.connect.once("open", function() {
console.log("Connected To MongoDB");
}).on("error", function(err) {
console.log("Error Connecting To MongoDB: ", err);
});
It's expected to output connected
, but it errors with MongoError: database name must be a string
.
3 Answers
Reset to default 13I needed to include the database name inside of the URI string. For example... mongodb+srv://MyUserName:[email protected]/MyDatabaseName
This will then point it to that specific database.
For whatever reason the new url parser doesn't seem to work with certain URLs.
As a quick fix you can try reverting back to the old one with { useNewUrlParser: false }
In your URI after where it says mongodb.net put slash and the name of the database for example:
mongoose.connect('mongodb+srv://<username>:<password>@<instance>.mongodb.net/**nameDatabase**', {dbName: "Eclipse"}, {useNewUrlParser: true})
mongoose.connect('mongodb+srv://MyUserName:[email protected]/', {dbName: 'yourDbName'});
Adding the dbname since you're using the mongodb+srv syntax – dillon.harless Commented Dec 19, 2018 at 20:18