0

I'm very new to Javascript and I am trying to connect to a mongodb database by following this guide.

const {MongoClient} = require('mongodb')

/*
Username: myusername
Key: mykey
*/

connString = 'mongodb+srv://<myusername>:<mykey>@' +
             'cluster0.vpss6.mongodb.net/<dbname>?' + 
             'retryWrites=true&w=majority'

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

databaseList = client.db().admin().listDatabases()

databaseList.then(function(result)  {

  console.log(result)

})

Whenever I omit the {} around the {MongoClient} line the code breaks I receive a Uncaught TypeError: client.db is not a function error message further down at the databaseList = client.db().admin().listDatabases() line. This also applies to when I switch the const declarations to var declarations.

Why is this?

uncool
  • 2,613
  • 7
  • 26
  • 55
  • 1
    The `{ }` in the declaration extract the property named "MongoClient" from the object exported by the library. – Pointy Sep 08 '20 at 12:51
  • 2
    Nodejs uses "commonJS". Try it like this `const MongoClient = require('mongodb').MongoClient` – bill.gates Sep 08 '20 at 12:54

0 Answers0