等待数据库删除,然后关闭连接会导致MongoExpiredSessionError。

huangapple go评论68阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月30日 02:56:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定