Getting Error this Error


Actually when I sent the request from Postman I am able to get the status Okay-- 200. But getting the following error in the terminal. Can anyone please help.
Error :
(node:59320) UnhandledPromiseRejectionWarning: MongoExpiredSessionError: Cannot use a session that has ended
at applySession (D:\MERN Stack\MongoDB_Refresher\node_modules\mongodb\lib\sessions.js:632:16)
at Connection.command (D:\MERN Stack\MongoDB_Refresher\node_modules\mongodb\lib\cmap\connection.js:186:53)
at D:\MERN Stack\MongoDB_Refresher\node_modules\mongodb\lib\sdam\server.js:189:18
at Object.callback (D:\MERN Stack\MongoDB_Refresher\node_modules\mongodb\lib\cmap\connection_pool.js:267:13)
at processWaitQueue (D:\MERN Stack\MongoDB_Refresher\node_modules\mongodb\lib\cmap\connection_pool.js:448:29)
at processTicksAndRejections (internal/process/task_queues.js:81:21)
(node:59320) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see Command-line API | Node.js v19.6.0 Documentation).

Hi @Mrudula_Kulkarni,

I believe you should await the result, i.e:

const result  = await ...

It is an asynchronous operation so result won’t have a defined value. In the next step you’re ending the connection before inserting.

SO-Related.


Edit/Comment: const result seems unnecessary. const result = await db.collection... could just become await db.collection...

If you want to test that the value is still undefined for the resolution, use console.log(result)

3 Likes

@santimir Thank you !! It worked also thanks for the suggestions.

1 Like