'mongoose.connection.close() not working correctly in my code

i dont know why but this closing this mongoose connection is not working.. even after doing everything correct i dont know why i am getting this error? i'm calling this function in 1-2 sec, maybe it is because the mongo client takes more time than that to close a connection? this is my function and mongo is the client i have made in separate file

module.exports.getCoins = async (guildId, userId) => {
  return await mongo().then(async (mongoose) => {
    try {
      console.log("running findone");

      const result = await profileSchema.findOne({ guildId, userId });

      console.log("result:", result);

      let coins = 0;
      if (result) {
        coins = result.coins;
      } else {
        console.log("inserting a doc");
        await new profileSchema({
          guildId,
          userId,
          coins,
        }).save();
      }

      return coins;
    }  finally {
      await mongoose.connection.close();
    }
  });

the error it is giving is

MongoExpiredSessionError: Cannot use a session that has ended
    at applySession (C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\sessions.js:632:16)
    at Connection.command (C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connection.js:186:53)
    at C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\sdam\server.js:189:18
    at Object.callback (C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connection_pool.js:267:13)
    at C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connection_pool.js:475:29
    at C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connection_pool.js:403:13
    at callback (C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connect.js:69:9)
    at C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connect.js:137:21
    at C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\auth\scram.js:163:20
    at MessageStream.messageHandler (C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connection.js:474:9)
MongoExpiredSessionError: Cannot use a session that has ended
    at applySession (C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\sessions.js:632:16)
    at Connection.command (C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connection.js:186:53)
    at C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\sdam\server.js:189:18
    at Object.callback (C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connection_pool.js:267:13)
    at C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connection_pool.js:475:29
    at C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connection_pool.js:403:13
    at callback (C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connect.js:69:9)
    at C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connect.js:137:21
    at C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\auth\scram.js:163:20
    at MessageStream.messageHandler (C:\Users\bhuvn\OneDrive\Desktop\bhuvnesh\S.D.E\node_modules\mongodb\lib\cmap\connection.js:474:9)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source