未处理的承诺拒绝:错误:URL 格式错误,无法解析

未处理的承诺拒绝:错误:URL 格式错误,无法解析

问题描述:

我同时是 aws 和 mongodb 的新手,所以我在尝试连接到托管在亚马逊 linux ec2 实例上的 mongo 数据库时遇到了一个非常基本的问题.原因是,我无法构建到我的数据库的路径.

I am new to aws and mongodb at the same time, so I'm stuck at a very basic point in trying to connect to my mongo databse, hosted on an amazon linux ec2 instance. The reason is, I'm not able to build the path to my database.

这是我尝试使用的:

mongoose.connect('mongod://ec2-user@ec2-XX-XX-XXX-XXX-XX.compute-1.amazonaws.com:27017/test' )

这是我测试 lambda 函数的结果:

And here is the result of my test lambda function:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: URL malformed, cannot be parsed

我使用的是 mongodb 3.6.5.

I'm using mongodb 3.6.5.

Mongoose 5.x 支持以下授权语法,并确保您没有在 url 中使用任何特殊字符,如 @,-,+,>

Mongoose 5.x supports following syntax for authorization and also make sure you have not used any special character in url like @,-,+,>

mongoose.connect(MONGO_URL, {
  auth: {
    user: MONGO_DB_USER,
    password: MONGO_DB_PASSWORD
  }
})

或者,如果您想删除弃用警告避免当前 URL 字符串解析器已弃用"

Or if you want to remove deprication warning Avoid "current URL string parser is deprecated"

添加选项useNewUrlParser

mongoose.connect(MONGO_URL, {
  auth: {
    user: MONGO_DB_USER,
    password: MONGO_DB_PASSWORD
  },
  { useNewUrlParser: true }
})