Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
MongoDB driver for node.js, transactions API, error "client.startSession() is not a function"

I am trying to use the MongoDB driver for node.js, for creating a transaction. Here is my code.

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://someone:someone@somecluster.xxxyy.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";

function dbConnect () {
    callback = callbackTrans;
    MongoClient.connect (uri, callback);
};

function callbackTrans (err, db) {
    if (err) throw err;
    var session = MongoClient.startSession();
    console.log ('so far so good');
};

dbConnect ();

It is incomplete code, of course. But I am getting the following error, when I run this program...

D:\NodeJS\node_modules\mongodb\lib\topologies\replset.js:339
          throw err;
          ^

TypeError: MongoClient.startSession is not a function
    at callbackTrans (D:\NodeJS\transaction.js:11:31)
    at D:\NodeJS\node_modules\mongodb\lib\utils.js:693:5
    at D:\NodeJS\node_modules\mongodb\lib\mongo_client.js:227:7
    at connectCallback (D:\NodeJS\node_modules\mongodb\lib\operations\connect.js:366:5)      
    at D:\NodeJS\node_modules\mongodb\lib\operations\connect.js:602:5
    at ReplSet.connectHandler (D:\NodeJS\node_modules\mongodb\lib\topologies\replset.js:336:9)
    at Object.onceWrapper (events.js:422:26)
    at ReplSet.emit (events.js:315:20)
    at D:\NodeJS\node_modules\mongodb\lib\core\topologies\replset.js:796:18
    at processTicksAndRejections (internal/process/task_queues.js:75:11)

What am I missing / doing incorrectly? Thanks.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Basically, MongoClient.startSession(); is invalid because startSession() is not a static method on MongoClient but an instance method which should be called on the client instance itself. MongoClient.connect() will pass the client instance in the callback and you should use it to invoke startSession() on it.

Try this:

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://someone:someone@somecluster.xxxyy.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";

function dbConnect () {
    callback = callbackTrans;
    MongoClient.connect (uri, callback);
};

function callbackTrans (err, client) {
    if (err) throw err;
    var session = client.startSession();
    console.log ('so far so good');
};

dbConnect();

References:

  1. connectCallback
  2. startSession method
over 4 years ago · Santiago Trujillo Report

0

You have to call the startSession method of the db parameter of the callback, this is what contains the method startSession if the connection is successful.

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://someone:someone@somecluster.xxxyy.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";

function dbConnect() {
    callback = callbackTrans;
    MongoClient.connect(uri, callback);
};

function callbackTrans(err, db) {
    if (err) throw err;
    var session = db.startSession();
    console.log ('so far so good');
};

dbConnect ();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!