报错:useCreateIndex is an invalid option

报错:useCreateIndex is an invalid option

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

https://img1.sycdn.imooc.com//climg/61bf00d909d580e611530341.jpg

相关代码:

/**

 * @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


相关代码:

搜索

复制

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,这边测试可能和mongoose版本有关系,课程中用的版本是

https://img1.sycdn.imooc.com//climg/6130969a09e52d3702890031.jpg

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

https://img1.sycdn.imooc.com//climg/613098bc09cdece709250136.jpg

https://img1.sycdn.imooc.com//climg/613096ba0928d94605300098.jpg

可以删除后再测试。

祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师