MongoDB连接时候出现提示: DeprecationWarning: current URL string parser is deprecated.

js中的代码如下所示,结果node.js运行时报错

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/playground')
	.then(() => console.log('数据库连接成功'))
	.catch(err => console.log(err, '数据库连接失败'))

错误提示:

(node:11540) 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.
(node:11540) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

mongoDB连接报错

出现这样的错误提示时,注意有两条提示,通过阅读分析得到

用警告:当前 URL 字符串解析器已弃用,将在将来的版本中删除。要使用新的解析器,请通过选项 { useNewUrlParser: true } 到 MongoClient. connect。

弃用警告:当前服务器发现和监视引擎已弃用,将在将来的版本中删除。要使用新的服务器发现和监视引擎,请将选项 { useUnifiedTopology: true } 传递给 MongoClient 构造函数。

也就是我们需要在代码块中写入这两条信息。

解决方案如下:

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true , useUnifiedTopology: true })
	.then(() => console.log('数据库连接成功'))
	.catch(err => console.log(err, '数据库连接失败'))

解决问题。
在这里插入图片描述
附:启动数据库和关闭数据库的命令

net start mongoDB  //启动数据库服务
net stop mongoDB  // 关闭数据库服务
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值