解决 mongoose DeprecationWarning 错误导致数据库连接不成功

mongoose 版本低提示错误为:

DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.

通常升级 mongoose 就可以解决:

npm install mongoose

 


 

 

连接数据库参数缺失提示错误:

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.

通常根据错误信息提示,将 useNewUrlparser 添加到 mongoose.connect 即可:

// db
const db = "mongodb://localhost:27017/blog";
mongoose.Promise = global.Promise
mongoose.connect(db, { useNewUrlParser: true }).then(() => console.log('Database Successful!')).catch((err) => console.log(err));

 

posted @ 2020-04-21 09:30  礼拜16  阅读(1255)  评论(0编辑  收藏  举报