Unable to establish a connection with the MongoDb client

I'm encountering an issue with connecting to MongoDB. The error message I'm receiving is:

 |         options.includeResultMetadata ??= false;
20:46:01 0|app    |                                       ^^^
20:46:01 0|app    | SyntaxError: Unexpected token '??='

The Node version I'm using for the project is: node -v => v20.7.0

MongoDB version: mongod --version => db version v6.0.8

Here's my package.json file:

"dependencies": {
    "compression": "^1.7.4",
    "cors": "^2.8.5",
    "express": "^4.18.2",
    "helmet": "^7.0.0",
    "http-status": "^1.7.0",
    "mongodb": "^6.1.0",
    "xss-clean": "^0.1.4"
  }
}

Can anyone help me figure out what I might be missing?

And here's a snippet of my code:

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


const uri = 'mongodb://localhost/testDatabase';

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

async function connectToDatabase() {
  try {
    await client.connect();
    console.log('Connected to the MongoDB database');
  } catch (err) {
    console.error('Error connecting to the MongoDB database:', err);
  }
}

module.exports = {
  connectToDatabase,
  getDatabase: () => client.db(),
};

Answer №1

let databaseUri = "mongodb://localhost:27017/testDatabase";

Answer №2

Encountered the same issue, but found a solution by downgrading the mongodb javascript module as suggested by @selfagency.

npm uninstall mongodb

then

npm i <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d2525292028252331447374777d">[email protected]</a>

You can find earlier versions here:

https://www.npmjs.com/package/mongodb/v/5.9.0?activeTab=versions

Don't forget to update your package.json dependencies:

"mongodb": "^6.1.0",

Change it to:

"mongodb": "^5.9.0",

Then reinstall your application's dependencies depending on whether you are deploying or developing locally.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The catch all route in Next.js seems to be malfunctioning when paired with the getStaticPaths function

Why is the Next.js catch all route not working as expected with the getStaticPaths function? The documentation states that I should be able to navigate to both t/a.cat and t/a.cat/a.id, but it only seems to work for the latter. What could be causing this ...

Arranging Array by Nearest Solution

I need help sorting an unsorted array containing calculation results: const solution = 955 const unsortedArray = [ [["solution result cab"],[955.709]], [["solution result abc"],[951.11]], [["solution result cba"],[954.709]], [["solution result bac ...

Troubleshooting issues with ASP.NET bundling and minification when using Angular.js

After reviewing many questions on the same topic, none of them were able to solve my specific case. Our current challenge involves bundling and minifying AngularJs files within .Net code. The following code snippet shows how we are bundling our files insi ...

Display handpicked ionic list view

I have put together a demonstration using this (demo) to showcase the issue I am facing. Within this demo, there is a page with a list, a button located in the header that checks the attribute of an <ion-checkbox>, and a checkbox next to the list v ...

Locate items within a collection using Django and mongodb

Although I am familiar with django, I am relatively new to mongodb. Below is the model I am working with: class Conversation(models.Model): date = models.DateTimeField(auto_now_add=True, db_index=True) users = ListField(models.CharField(max_length ...

Guide to synchronizing schema changes for legacy document collections in MongoDB using default values

Is there a way to implement schema changes in mongodb and sync them with default values for all existing data? import mongoose from "mongoose"; interface ITodo { title: string; description: string; by: string; } interface todoModelInterfa ...

Responsive menu not collapsing properly and appearing funky in Drupal

I've managed to create a responsive navigation bar, but I'm encountering two issues. Firstly, when I resize the screen on my test pages, not all of the links hide as expected. Secondly, after inserting the code into Drupal, the nested links appea ...

Tips on choosing and showcasing information from jQuery date and time picker

I am struggling to show the selected data from a jQuery date and time picker and save it to a database using PHP with MySQL. I am not sure how to retrieve the information. Here is the jQuery code for the date and time picker along with a suggested jQuery f ...

Converting Hexadecimal Values to Base32-Encoding Using Javascript

I'm encountering a problem with converting a function from Ruby to Javascript (specifically node.js, but I prefer a solution that is compatible with browsers, if possible). Here is the hex-formatted sha256 digest: "0b08dfe80a49490ae0722b9306ff53c5ab ...

Find the sum and subtotals for two different categories in a JavaScript array

Currently, I'm in the process of aggregating three totals: totalPoints, monthlyTotals, and monthlyByType by utilizing Array.prototype.reduce. So far, I've managed to successfully calculate totalPoints and monthlyTotals, but I'm encountering ...

The absence of onreadystatechange() on XMLHttpRequest objects is proving to be problematic

(function (updateSend) { XMLHttpRequest.prototype.send = function () { console.log(this.onreadystatechange); //null updateSend.apply(this, arguments); }; })(XMLHttpRequest.prototype.send); Why does this.onreadystatechange ev ...

Turn off Appbar padding at the top and bottom

I am looking to create a customized box within the Appbar that spans the full height, as illustrated in the image below: https://i.stack.imgur.com/CFMo0.jpg However, the default Appbar provides padding from all sides to its internal elements, leading to ...

Defining preset values within a TypeScript model class

Suppose I have a User class and I want to instantiate only predefined 'status'. Is this the optimal approach? Are there any other alternatives? What is the correct way to achieve this? Thank you in advance. export class User { constructor( ...

Display or conceal objects within a 3D model with an HTML checkbox

I have utilized JavaScript, CSS, and Three.js to create a 3D model. The project comprises a total of 5 JS files, 1 CSS file, and 1 HTML file. Here is an overview of the model: [] Showers have been incorporated for each cubicle, which should only be vis ...

PHP: run a function when a button is clicked

I am currently developing a PHP & mySQLi CRUD application. Within a functions.php file, I have implemented a function that handles deleting individual users: function delte_single_user() { if (isset($_GET['id'])) { global $con; $us ...

Incorporating various criteria within a function

Struggling to add 2 conditions in 1 function, need some guidance. Here's the code I have: var timeCheck = function(){ var timePos = videoBGPlayer.currentTime; if (timePos > 1){ TweenLite.to(videoBGPlayer, 3, { marginTop: -130}); ...

What could be causing my template to not update when it's bound to a computed property?

Currently, I am encountering an issue where a component's template HTML depends on the computed getter of a Vuex method. The template is designed to display the output of the computed property within a <p> tag using {{ getNumSets }}. Upon updat ...

What is the best way to utilize Criteria for querying mongoDb in Java?

For my spring-boot application, I am utilizing MongoDB. I have a need to retrieve a list of documents based on a set of IDs. In my database, there is a collection named Role, and I have a list called roleIdList. This list contains all the role IDs for whi ...

Different scenarios call for different techniques when it comes to matching text between special characters

I encounter different scenarios where strings are involved. The goal is to extract the text in between the || symbols. If there is only one ||, then the first part should be taken. For example: Useless information|| basic information|| advanced informa ...

The glyphicons in Bootstrap are not appearing

My angular app is built using bulp angular and the component. I incorporate Sass (Node) into the project. I decided to switch to the flatly theme, which I downloaded from the bootswatch website and inserted it into _bootstrap.scss. // Core variables a ...