Details
-
Task
-
Status: Closed
-
Minor - P4
-
Resolution: Fixed
-
None
-
None
-
1
-
CET: Vicuña (11 - 17 Jan 22)
Description
On the documentation page for "How to use Facets with Atlas Search" (https://docs.atlas.mongodb.com/atlas-search/tutorial/facet-tutorial/), there is a tutorial that provides some sample code for trying out stringfacets. As part of the tutorial, you can choose the language that the sample code is displayed in. If you select Node and copy and paste the sample code provided, it will not work - you will get a MongoExpiredSessionError such as below:
/Users/john/code/apidemo/node_modules/mongodb/lib/sessions.js:647 |
return new error_1.MongoExpiredSessionError(); |
^MongoExpiredSessionError: Cannot use a session that has ended
|
at applySession (/Users/john/code/apidemo/node_modules/mongodb/lib/sessions.js:647:16) |
at Connection.command (/Users/john/code/apidemo/node_modules/mongodb/lib/cmap/connection.js:185:53) |
at /Users/john/code/apidemo/node_modules/mongodb/lib/sdam/server.js:183:18 |
at Object.callback (/Users/john/code/apidemo/node_modules/mongodb/lib/cmap/connection_pool.js:266:13) |
at processWaitQueue (/Users/john/code/apidemo/node_modules/mongodb/lib/cmap/connection_pool.js:447:29) |
at processTicksAndRejections (node:internal/process/task_queues:82:21)Node.js v17.0.1 |
__
The reason is because in the sample code, they iterate through the result set with a forEach loop, but they do not mark it with await. This means that the program continues onto the finally block where the connection is closed before the output is finished. By adding an ** await, the issue is resolved. That section of the code should read as follows:
// run pipeline
|
const result = coll.aggregate(agg); |
|
// print results
|
await result.forEach((doc) => console.dir(JSON.stringify(doc)));
|
} finally { |
await client.close();
|
}
|