UnhandledPromiseRejectionWarning:错误:URI 没有主机名、域名和 tld

use*_*875 3 javascript mongoose mongodb node.js

我正在学习 Wes Bos 的教程,学习 Node.js。

我下载了所有的启动文件并使用 MongoDb Atlas 创建了一个数据库。

当我运行 npm start 时,出现此错误:

UnhandledPromiseRejectionWarning: Error: URI does not have hostname, domain name and tld
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.
Run Code Online (Sandbox Code Playgroud)

我正在使用环境变量连接到数据库

mongoose.connect(process.env.DATABASE);
Run Code Online (Sandbox Code Playgroud)

变量如下所示:

DATABASE=mongodb+srv://<username>:<password>@cluster1-rgvum.mongodb.net/test?retryWrites=true
Run Code Online (Sandbox Code Playgroud)

出于显而易见的原因,我省略了我的用户名和密码。如果有人知道如何进行,将不胜感激!谢谢你。

小智 7

是的,是因为密码中使用了特殊字符,只需使用这种简单的方法,或者使用上述复杂的代码进行编码。

在密码中使用十六进制代码而不是您在密码中使用的符号,就像您的文本密码是:- pass123#

所以像这样使用它:- pass123%23

从这里获取 ASCII 代码并使用十六进制代码:- https://ascii.cl/

有关更多详细信息:- https://docs.atlas.mongodb.com/troubleshoot-connection/#special-characters-in-connection-string-password