Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-15031

[Atlas] Bug in Node sample code for Atlas Search Facet Tutorial

    • Type: Icon: Task Task
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Atlas
    • Labels:
    • 1

      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:


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

            james.sanchez@mongodb.com James Sanchez (Inactive)
            john.misczak@mongodb.com John Misczak
            0 Vote for this issue
            1 Start watching this issue

              2 years, 23 weeks, 1 day ago