William
William

Reputation: 333

'database name must be a string' Error when connecting with my Connection 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.

Upvotes: 5

Views: 10639

Answers (3)

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})

Upvotes: 2

William
William

Reputation: 333

I 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.

Upvotes: 13

Sherlock
Sherlock

Reputation: 5627

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 }

Upvotes: 6

Related Questions