Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-2401

"readPreference.equals is not a function" exception thrown within executeWithServerSelection

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 3.5.1
    • Affects Version/s: 3.3.0, 3.3.1, 3.3.2, 3.3.3, 3.3.4, 3.3.5, 3.4.0, 3.4.1
    • Component/s: None

      I've just upgraded to version 3.3.0 from version 3.2.7 and receive the following exception:

       

      TypeError: readPreference.equals is not a function
      at executeWithServerSelection (<PATH_TO_SRC>/node_modules/mongodb/lib/operations/execute_operation.js:125:40)
      at executeOperation (<PATH_TO_SRC>/node_modules/mongodb/lib/operations/execute_operation.js:92:16)
      at AggregationCursor._initializeCursor (<PATH_TO_SRC>/node_modules/mongodb/lib/core/cursor.js:545:7)
      at AggregationCursor._initializeCursor (<PATH_TO_SRC>/node_modules/mongodb/lib/cursor.js:191:11)
      at nextFunction (<PATH_TO_SRC>/node_modules/mongodb/lib/core/cursor.js:748:10)
      at AggregationCursor._next (<PATH_TO_SRC>/node_modules/mongodb/lib/core/cursor.js:202:5)
      at fetchDocs (<PATH_TO_SRC>/node_modules/mongodb/lib/operations/to_array.js:27:14)
      at ToArrayOperation.execute (<PATH_TO_SRC>/node_modules/mongodb/lib/operations/to_array.js:60:5)
      at <PATH_TO_SRC>/node_modules/mongodb/lib/operations/execute_operation.js:109:26
      at new Promise (<anonymous>)

       

      The exception arises from using an aggregate function and calling .toArray() on the result:

       

      unprocessedCompanyAggregate = await getCollection('company').aggregate([
         {
            '$match': {
               '_id' : createIDForDatabase(companyID)
            }
         },
         {
            '$project': {
               'financialPeriod': {
                  '$arrayElemAt': ['$financialPeriods', financialPeriodsIndex]
               }
            }
         },
         {
            '$project': {
               'statementsName': '$financialPeriod.statementsType.name'
            }
         }
      ],{
         'session': session
      }).toArray(); // toArray needed so converts the cursor for the aggregation to an array of documents.
      

       

      The problem bit of mongodb code is in execute_operation.js:

       

      const readPreference = operation.readPreference || ReadPreference.primary;
      const inTransaction = operation.session && operation.session.inTransaction();
      if (inTransaction && !readPreference.equals(ReadPreference.primary)) {
      ...
      }

      The only time this exception seems to occur is when operation.readPreference exists.

       

      I'm using transactions, and the only time I'm setting readPreference is when starting a transaction:

       

      session.startTransaction({
         'writeConcern': {
            'w': WRITE_CONCERN_W_MAJORITY,
            'j': WRITE_CONCERN_J_TRUE,
            'wtimeout': WRITE_CONCERN_WTIMEOUT_1000,
         },
         'readConcern': {
            'level': 'snapshot'
         },
         'readPreference': {
            'mode': 'primary'
         }
      });
      

       

      Where 

      const WRITE_CONCERN_W_MAJORITY = 'majority';
      const WRITE_CONCERN_J_TRUE = true;
      const WRITE_CONCERN_WTIMEOUT_1000 = 1000;
      

       

       Any ideas what is going on?

       

            Assignee:
            matt.broadstone@mongodb.com Matt Broadstone
            Reporter:
            sjom106 Sam Miller
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: