[DOCS-15031] [Atlas] Bug in Node sample code for Atlas Search Facet Tutorial Created: 13/Jan/22  Updated: 29/Oct/23  Resolved: 14/Jan/22

Status: Closed
Project: Documentation
Component/s: Atlas
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Minor - P4
Reporter: John Misczak Assignee: James Sanchez (Inactive)
Resolution: Fixed Votes: 0
Labels: cet-captain
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 2 years, 3 weeks, 5 days ago
Story Points: 1

 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();    
}



 Comments   
Comment by John Misczak [ 14/Jan/22 ]

After viewing the change, it looks good to me

Generated at Thu Feb 08 08:11:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.