报错:useCreateIndex is an invalid option
我在测试连接数据库时报错了。。请问老师是怎么回事呢?

相关代码:
/**
* @description mongoose 连接数据库
* @author 双越老师
*/
const mongoose = require('mongoose')
const url = 'mongodb://localhost:27017' // 本地默认的 mongodb 服务地址
const dbName = 'testdb' // 数据库名称
// 解决提示 DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
mongoose.set('useCreateIndex', true)
// 解决提示 DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated.
mongoose.set('useFindAndModify', false)
// 开始连接
mongoose.connect(`${url}/${dbName}`, {
useNewUrlParser: true,
useUnifiedTopology: true
})
// 连接对象
const db = mongoose.connection
db.on('error', err => {
console.error('mongoose connect error', err)
})
db.once('open', () => {
// 用以测试数据库连接是否成功
console.log('mongoose connect success')
})
module.exports = mongoose
相关代码:
搜索
复制
正在回答
同学你好,这边测试可能和mongoose版本有关系,课程中用的版本是

现在安装最新的应该是6版本,在mongoose6中,不需要这两行配置了


可以删除后再测试。
祝学习愉快!
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星