skip to Main Content

I am new to MongoDB and Node.js. I am following the MongoDB documentation and am currently unable to connect to the MongoDB Cluster using Node.js.

I am able to successfully establish a connection using the Mongo Shell as below.

mongosh "mongodb+srv://my-default-cluster.nopctvg.mongodb.net/" --apiVersion 1 --username sandeepc

Output

Enter password: ***************
Current Mongosh Log ID: 650c92440bef28a12785503b
Connecting to:      mongodb+srv://<credentials>@my-default-cluster.nopctvg.mongodb.net/?appName=mongosh+2.0.1
Using MongoDB:      6.0.10 (API Version 1)
Using Mongosh:      2.0.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

Atlas atlas-cjxqji-shard-0 [primary] test> show dbs
sample_airbnb        52.89 MiB
sample_analytics      9.57 MiB
sample_geospatial     1.28 MiB
sample_guides        40.00 KiB
sample_mflix        111.21 MiB
sample_restaurants    6.57 MiB
sample_supplies       1.05 MiB
sample_training      49.94 MiB
sample_weatherdata    2.60 MiB
admin               332.00 KiB
local                31.98 GiB
Atlas atlas-cjxqji-shard-0 [primary] test>

However, when I run the following code, it always gives me the same error. The password enclosed within the parentheses is replaced by the actual password.

Code

const {MongoClient} = require('mongodb');

const uri = 'mongodb+srv://sandeepc:<password>@my-default-cluster.nopctvg.mongodb.net/?retryWrites=true&w=majority';

const database = 'sample_mflix';

const client =  new MongoClient(uri, {useUnifiedTopology: true});


async function getdata(){

    let result = await client.connect();

    let db = result.db(database);

    let collection = db.collection('movies');

    let response = await collection.log(collection.find({}).toArray());

    console.log(response);
}

getdata();

Error:

/home/sandeep/vs-workspace/nodejs-mongodb-quickstart/node_modules/mongodb/lib/admin.js:62
            session: options?.session,
                             ^

SyntaxError: Unexpected token '.'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/sandeep/vs-workspace/nodejs-mongodb-quickstart/node_modules/mongodb/lib/index.js:6:17)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)

MongoDB Version Used : 6.0.10
NodeJS Version Used : 12.22.9
NPM Version : 8.5.1

I hope I am not missing something basic. Not sure what could be wrong here.

2

Answers


  1. The specific error is SyntaxError: Unexpected token ‘.’, which typically means that you’re trying to use a feature or syntax that is not supported in the version of Node.js you’re running.

    To fix this issue, you have a few options:

    1. Upgrade Node.js: Make sure you’re using a version of Node.js that
      supports optional chaining (version 14 or higher).

    2. Update Your Code: If you can’t upgrade Node.js for some reason,
      you’ll need to update your code to use a different syntax that is
      compatible with your current Node.js version.

    3. Use Babel or a Transpiler: If you want to use modern JavaScript
      features without worrying about Node.js compatibility, you can set
      up a transpiler like Babel to automatically convert your code to a
      compatible version.

    4. Check for Typos: Make sure there are no syntax errors or typos in
      your code, especially around the area indicated by the error
      message.

    Login or Signup to reply.
  2. NodeJS doesn’t support most of optional chaining until version 14.5. And it doesn’t support spread parameters after optional chaining until 16.3. You need to upgrade if you want to use it. Or use some sort of tool to downlevel your code (Ex. Babel).

    https://node.green/#ES2020-features-optional-chaining-operator—–

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search