英文:
Awaiting database drop, then closing connection gives MongoExpiredSessionError
问题
我正在使用Jest运行测试。我使用afterAll()
函数来删除数据库,然后关闭连接。但是,我收到了一个"MongoExpiredSessionError"错误。
相关代码:
afterAll(async () => {
await mongoose.connection.db.dropDatabase((err) => { console.error(err) });
mongoose.disconnect();
});
错误信息:
MongoExpiredSessionError: 无法使用已结束的会话
at applySession (/home/leemorgan/projects/js/api-easycv-work/node_modules/mongodb/src/sessions.ts:978:12)
at Connection.command (/home/leemorgan/projects/js/api-easycv-work/node_modules/mongodb/src/cmap/connection.ts:519:31)
at /home/leemorgan/projects/js/api-easycv-work/node_modules/mongodb/src/sdam/server.ts:365:14
at Object.callback (/home/leemorgan/projects/js/api-easycv-work/node_modules/mongodb/src/cmap/connection_pool.ts:513:7)
at ConnectionPool.processWaitQueue (/home/leemorgan/projects/js/api-easycv-work/node_modules/mongodb/src/cmap/connection_pool.ts:689:25)
at /home/leemorgan/projects/js/api-easycv-work/node_modules/mongodb/src/cmap/connection_pool.ts:357:33
at processTicksAndRejections (node:internal/process/task_queues:77:11) {
[Symbol(errorLabels)]: Set(0) {}
}
在我看来,disconnect
应该在数据库被删除之前运行,因为我正在等待。为什么会发生这种情况,我该如何修复呢?
英文:
I am using Jest to run tests. I use the afterAll()
function to drop the database, then closing the connection. However, I am receiving a "MongoExpiredSessionError".
Relevant code:
afterAll(async ()=>{
await mongoose.connection.db.dropDatabase((err)=>{console.error(err)});
mongoose.disconnect();
});
Error:
MongoExpiredSessionError: Cannot use a session that has ended
at applySession (/home/leemorgan/projects/js/api-easycv-work/node_modules/mongodb/src/sessions.ts:978:12)
at Connection.command (/home/leemorgan/projects/js/api-easycv-work/node_modules/mongodb/src/cmap/connection.ts:519:31)
at /home/leemorgan/projects/js/api-easycv-work/node_modules/mongodb/src/sdam/server.ts:365:14
at Object.callback (/home/leemorgan/projects/js/api-easycv-work/node_modules/mongodb/src/cmap/connection_pool.ts:513:7)
at ConnectionPool.processWaitQueue (/home/leemorgan/projects/js/api-easycv-work/node_modules/mongodb/src/cmap/connection_pool.ts:689:25)
at /home/leemorgan/projects/js/api-easycv-work/node_modules/mongodb/src/cmap/connection_pool.ts:357:33
at processTicksAndRejections (node:internal/process/task_queues:77:11) {
[Symbol(errorLabels)]: Set(0) {}
}
It seems to me that disconnect
should not run until the database is dropped because I am awaiting. Why is this happening and how do I fix it?
答案1
得分: 2
Often dual use API's that are Promise based & callback based flip between returning a promise based on if you supply a callback.
So ->
await mongoose.connection.db.dropDatabase((err)=>{console.error(err)});
In the above a callback was supplied, the API then thought you were using the callback variant, so it didn't bother wrapping it inside a Promise. This then causes the await
to do nothing.
英文:
Often dual use API's that are Promise based & callback based flip between returning a promise based on if you supply a callback.
So ->
await mongoose.connection.db.dropDatabase((err)=>{console.error(err)});
In the above a callback was supplied, the API then thought you was using callback variant so didn't bother wrapping inside a Promise, this then causes the await
to do nothing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论