MongoParseError: Invalid connection string

半腔热情 提交于 2020-08-24 10:24:17

问题


I am trying to connect MongoDB database with this code but when running it I get the error (see the error below after the code). The initial error was in the line where it was resolved by adding useNewUrlParser: true but even after this I still get more errors. I am using MongoDB version 4.0.1. Does anybody know how to resolve this error?

mongoose.connect('User://localhost:27017/User',{ useNewUrlParser: true })

Error while running this code:

(node:11068) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): MongoParseError: Invalid connection string (node:11068) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.


回答1:


Instead of User, use 'mongodb://localhost/' I had the same problem.




回答2:


The host you have written is not correct, and it should be

mongoose.connect('mongodb://localhost:27017/User',{ useNewUrlParser: true })



回答3:


Try this and it should work,

mongoose.connect('mongodb://localhost/mycargarage', {useNewUrlParser: true, useUnifiedTopology: true})
    .then(() => console.log('MongoDB Connected...'))
    .catch((err) => console.log(err))



回答4:


I was receiving the same error, then I used:

mongoose.connect("mongodb://localhost:27017/[yourDbName]", {
  useUnifiedTopology: true,
  useNewUrlParser: true
});

Substitute [yourDbName] for your MongoDB database's name:




回答5:


I had this same issue. In my case, the issue was caused by my password. Apparently, if there are special characters in the password, you need to use the HEX value.



来源:https://stackoverflow.com/questions/54721216/mongoparseerror-invalid-connection-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!