最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 'database name must be a string' Error when connecting with my Connection String - Stack Overflow

programmeradmin3浏览0评论

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.

Share Improve this question asked Dec 19, 2018 at 17:45 WilliamWilliam 3331 gold badge4 silver badges14 bronze badges 1
  • 1 try this: 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
Add a comment  | 

3 Answers 3

Reset to default 13

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.

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})
发布评论

评论列表(0)

  1. 暂无评论