i am trying to get a string from all documents in a mongo db collection, but when it starts it give me an error:
106 | * not subject to semantic versioning compatibility guarantees and may change at any time.
107 | *
108 | * @public
109 | **/
110 | constructor(message, options) {
111 | super(message, options);
^
error: Cannot use a session that has ended
at new MongoError (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodbliberror.js:111:9)
at new MongoDriverError (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodbliberror.js:209:9)
at new MongoAPIError (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodbliberror.js:238:9)
at new MongoExpiredSessionError (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodbliberror.js:403:9)
at applySession (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodblibsessions.js:708:16)
at prepareCommand (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodblibcmapconnection.js:169:49)
at C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodblibcmapconnection.js:259:30
at C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodblibcmapconnection.js:313:26
at command (C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodblibcmapconnection.js:311:23)
at C:UsersUtenteDesktopSkyForce Helpernode_modulesmongodblibsdamserver.js:167:40
my function:
export async function returnAllRep(): Promise<string[]> {
const { client, database } = await connectToDatabase();
const collection = database.collection('chains');
try {
const docs = await collection.find().toArray();
const ids = docs.map(doc => doc.id);
return ids;
} catch (error) {
console.error('Errore durante la recuperazione dei documenti:', error);
throw error;
} finally {
await client.close();
}
}
I have trying to add debug when it connect to the db, it says it’s connected but then it crash
New contributor
Matteo Donati is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.