node.js - 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
。帮我解决这个错误
解决方案
恐怕你不能像这样放弃收藏:
db.User.drop()
如果你想放弃收藏,那么你应该这样做:
mongoose.connection.db.dropCollection('User', function(err, result) {...});
推荐阅读
- python - 行星倾角,用python沿着轨道跟踪旋转轴
- linux - 如何在文件名(也是子目录)中用一个空格替换双空格(CloudLinux Server 6.10 版)
- bash - 将 bash 命令重构为 tcsh 命令
- postgresql - PostgreSQL 的可重复读取允许幻读但它的文档说它不允许
- c# - 请将 SQL 转换为 LINQ 或改进我的查询
- javascript - 方形表单构建不填充 IOS 设备上的字段(Phonegap 应用程序)
- typo3 - 有没有办法在typo3 6.2.31中添加登录框
- java - 如何在 Java 中使用 Base64 对特殊字符进行编码
- flask - Flask 不使用 url_for() 加载 css
- python - 为什么函数中的列表副本不起作用?