'database name must be a string' Error when connecting with my Connection String

Viewed 7622

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:MyPassword@williamdata7kmxm.mongodb.net", {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

I needed to include the database name inside of the URI string. For example... mongodb+srv://MyUserName:MyPassword@williamdata7kmxm.mongodb.net/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})
Related