首页 > 解决方案 > TypeError:无法读取未定义的属性“drop”

问题描述

我正在做摩卡测试。我必须在函数中连接到 MongoDB,before并且需要在函数后删除集合中的文档。

before("authenticate user", async () => {
        mongoose.connect('mongodb://localhost:27017/mo-identity')
        db = mongoose.connection;
        db.once('open', function() {
            console.log('We are connected to test `enter code here`database!')
        })
        .on('error', ()=>{console.error.bind(console, 'connection error')})
        })

    after(()=>{
        db.User.drop()
    })

以上是我的代码。 user是一个集合。执行此代码时,我收到此错误TypeError: Cannot read property 'drop' of undefined。帮我解决这个错误

标签: node.jsmongodbmongoosemocha.jschai

解决方案


恐怕你不能像这样放弃收藏:

db.User.drop()

如果你想放弃收藏,那么你应该这样做: mongoose.connection.db.dropCollection('User', function(err, result) {...});


推荐阅读