当前的URL字符串解析器已被弃用。

19
当我运行"node app.js"命令时,会出现以下错误:(node:2509) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

这是一个警告。同时,它提供了修复该问题的信息。我不确定您在询问什么。 - njzk2
3个回答

32
你需要向 mongoose.connect() 方法传递 { useNewUrlParser: true } 选项。你的代码应该类似于下面的片段。
mongoose.connect('mongodb://user:pw@host1.com:27017/dbname', { useNewUrlParser: true })

4
当我制作这个时,它会给我一个错误 <code>UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): [object Object] (node:7) 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.</code> 即使我没有提到任何与promise相关的内容。 - palAlaa
@palAlaa 当我在urlcallback之间作为第二个参数传递{useNewUrlParser:true}时,我遇到了相同的错误。 - Phillip Berger

5
为了避免这个问题,请使用以下内容。
MongoClient.connect('mongodb://user:password@sample.com:port/dbname', { useNewUrlParser: true })

我应该在哪个文件中使用这段代码片段? - Nompumelelo

0

对于那些出现“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”这个消息的人,即使我没有提及与promise有关的任何内容。

我将代码粘贴在以下链接中,错误已被修复。 https://stackoverflow.com/a/52459082/10399142


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接