mongoose报错:DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead

参考:mongoose报错:DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead

 

mongoose报错:
(node:9716)DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead

 

解决方法:

// 导入第三方模块mongoose
const mongoose = require('mongoose');

// mongoose.set('useCreateIndex', true) //加上这个
// 或连接数据库时添加 useCreateIndex: true
 mongoose.connect('mongodb://Jacey:123@localhost:27017/blog2', { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true }) .then(() => console.log('数据库链接成功')) .catch(err => console.log('数据库连接失败:', err));

 

 

 

posted @ 2020-08-29 11:07  JaceyKan  阅读(413)  评论(0)    收藏  举报