未处理的承诺拒绝:错误:URL 格式错误,无法解析 [英] Unhandled promise rejection: Error: URL malformed, cannot be parsed

查看:42
本文介绍了未处理的承诺拒绝:错误: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 }
})

这篇关于未处理的承诺拒绝:错误:URL 格式错误,无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆