mongoose.connect()方法在powershell中报错
报错内容:
网站服务器连接成功,请访问localhost
(node:14116) Warning: Accessing non-existent property ‘MongoError’ of module exports inside circular dependency
(Use node --trace-warnings ...
to show where the warning was created)
(node:14116) 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:14116) 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.
(node:14116) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version.
(node:14116) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
数据库连接成功
原因:
第一个报错:(node:14116) Warning: Accessing non-existent property ‘MongoError’ of module exports inside circular dependency 是因为MongoDB的底层报错可忽略等待版本更新解决。
其余报错是因为字符串解析器报错需要在mongoose.connect()方法中传入报错中提示的对象参数。
解决方案:
// 引入mongoose第三方模块
const mongoose = require('mongoose');
//连接数据库
mongoose.connect('mongodb://localhost/blog', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
.then(() => console.log('数据库连接成功'))
.catch(() => console.log('数据库连接失败'))