Mongo connection error

Determining test suites to run…Setup Mongo Connection
FAIL test/lessons/mongoclient.spec.js
● Test suite failed to run

TypeError: Cannot read property 'close' of undefined

  17 | 
  18 |   async teardown() {
> 19 |     await this.global.mflixClient.close()
     |                                   ^
  20 |     await super.teardown()
  21 |   }
  22 | 

  at MongoEnvironment.teardown (test/config/mongoEnvironment.js:19:35)
      at Generator.throw (<anonymous>)

Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.259s
Ran all test suites matching /mongoclient/i.
Teardown Mongo Connection
npm ERR! Test failed. See above for more details.

Hi @mahendrakumar_30972,

Please make sure you have updated the .env file with your MFLIX_DB_URI correctly.

If that does not resolve, please share the contents of your .env file.

Kanika

Hello, I have the same issue,
Here is the .env

Ticket: Connection

Rename this file to .env after filling in your MFLIX_DB_URI and your SECRET_KEY

Do not surround the URI with quotes

SECRET_KEY=hollingsworth
MFLIX_DB_URI=mongodb+srv://m220student:m220password@mflix-2hjcq.mongodb.net
MFLIX_NS=mflix
PORT=5000

What is the SECRET_KEY do is it used?

The atlas cluster has badly configured DNS (missing SRV records)
Don’t use +srv. It gives error
mongo mongodb+srv://m220student:m220password@m220-shard-00-00-3p36m.mongodb.net
DNSHostNotFound: Failed to look up service “_mongodb._tcp.m220-shard-00-00-3p36m.mongodb.net”
Instead of it use
mongo mongodb://m220student:m220password@m220-shard-00-00-3p36m.mongodb.net/?ssl=true

Hi @hollingsworth.robert,

Please make sure you are able to connect to the atlas cluster from your system. Please run the below command from your terminal or command line and if you get any issues, check for:

  • Firewall

  • IP whitelisting from your cluster

    mongo mongodb+srv://m220student:m220password@mflix-2hjcq.mongodb.net
    

Kanika

Kanika,

Yes, I was able
to connect from the command line.

c:\university\m220\mflix-js>mongo
mongodb+srv://m220student:m220password@mflix-2hjcq.mongodb.net
MongoDB shell
version v4.0.9
connecting to:
mongodb://mflix-shard-00-02-2hjcq.mongodb.net.:27017,mflix-shard-00-00-2hjcq.mongodb.net.:27017,mflix-shard-00-01-2hjcq.mongodb.net.:27017/?authSource=admin&gssapiServiceName=mongodb&replicaSet=mflix-shard-0&ssl=true
2019-06-21T10:46:25.895-0400
I NETWORK [js] Starting new replica set monitor for mflix-shard-0/mflix-shard-00-02-2hjcq.mongodb.net.:27017,mflix-shard-00-00-2hjcq.mongodb.net.:27017,mflix-shard-00-01-2hjcq.mongodb.net.:27017
2019-06-21T10:46:26.222-0400
I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected
to mflix-shard-00-02-2hjcq.mongodb.net.:27017 (1 connections now open to
mflix-shard-00-02-2hjcq.mongodb.net.:27017 with a 5 second timeout)
2019-06-21T10:46:26.222-0400
I NETWORK [js] Successfully connected to mflix-shard-00-01-2hjcq.mongodb.net.:27017
(1 connections now open to mflix-shard-00-01-2hjcq.mongodb.net.:27017 with
a 5 second timeout)
2019-06-21T10:46:26.544-0400
I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected
to mflix-shard-00-00-2hjcq.mongodb.net:27017 (1 connections now open to
mflix-shard-00-00-2hjcq.mongodb.net:27017 with a 5 second timeout)
2019-06-21T10:46:26.599-0400
I NETWORK [js] Successfully connected to mflix-shard-00-00-2hjcq.mongodb.net.:27017
(1 connections now open to mflix-shard-00-00-2hjcq.mongodb.net.:27017 with
a 5 second timeout)
2019-06-21T10:46:26.601-0400
I NETWORK [ReplicaSetMonitor-TaskExecutor] changing hosts to mflix-shard-0/mflix-shard-00-00-2hjcq.mongodb.net:27017,mflix-shard-00-01-2hjcq.mongodb.net:27017,mflix-shard-00-02-2hjcq.mongodb.net:27017
from mflix-shard-0/mflix-shard-00-00-2hjcq.mongodb.net.:27017,mflix-shard-00-01-2hjcq.mongodb.net.:27017,mflix-shard-00-02-2hjcq.mongodb.net.:27017
2019-06-21T10:46:26.822-0400
I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected
to mflix-shard-00-01-2hjcq.mongodb.net:27017 (1 connections now open to
mflix-shard-00-01-2hjcq.mongodb.net:27017 with a 5 second timeout)
2019-06-21T10:46:27.041-0400
I NETWORK [js] Successfully connected to mflix-shard-00-00-2hjcq.mongodb.net:27017
(1 connections now open to mflix-shard-00-00-2hjcq.mongodb.net:27017 with
a 0 second timeout)
2019-06-21T10:46:27.083-0400
I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected
to mflix-shard-00-02-2hjcq.mongodb.net:27017 (1 connections now open to
mflix-shard-00-02-2hjcq.mongodb.net:27017 with a 5 second timeout)
Implicit session:
session { “id” : UUID(“b6c05d81-db17-4541-b4fe-07855986d3b2”)
}
MongoDB server
version: 4.0.10
Error while trying
to show server startup warnings: user is not allowed to do action [getLog]
on [admin.]
MongoDB Enterprise
mflix-shard-0:PRIMARY>

Robert J. Hollingsworth
Technical Delivery
Manager
IBM Client Inovation
Center: Lansing
Phone: 586-201-7112
Mobile

From:
Kanika
Singla via MongoDB University Discussion Forum mongodb_university@discoursemail.com
To:
rholling@us.ibm.com
Date:
06/21/2019
10:32 AM
Subject:
[EXTERNAL]
[MongoDB University Discussion Forum] [M220JS: MongoDB for Javascript Developers]
Mongo connection error

Same problem. Mongo shell connection is fine not fail when connecting by javascript.

Determining test suites to run…Setup Mongo Connection
FAIL test/lessons/basic-reads.spec.js
● Test suite failed to run

TypeError: Cannot read property 'close' of undefined

  17 | 
  18 |   async teardown() {
> 19 |     await this.global.mflixClient.close()
     |                                   ^
  20 |     await super.teardown()
  21 |   }
  22 | 

Here is my doting_unix file.
SECRET_KEY=super_secret_key_you_should_change
MFLIX_DB_URI=mongodb+srv://m220student:m220password@mflix-5uy0f.mongodb.net
MFLIX_NS=mflix
PORT=5000

Hi @Deep_89036,

The filename should be .env and not doting_unix.

mv dotenv_unix .env  # on Unix
ren dotenv_win .env  # on Windows 

Kanika

Same issue here.

I can connect with the client

mongo --uri "mongodb+srv://m220student:m220password@cluster0-5qsjr.mongodb.net/sample_mflix"

But I always get the same error when trying to

npm test -t mongoclient

Here is my .env file

SECRET_KEY=super_secret_key_you_should_change
MFLIX_DB_URI=mongodb+srv://m220student:m220password@cluster0-5qsjr.mongodb.net
MFLIX_NS=sample_mflix
PORT=5000

Hi @Flavio_79937,

It would be great if you can share the error and whole file contents for .env file. It must be having [TEST] and [PROD] keys as well. Make sure you are changing URL everywhere.

Kanika

I even tryied to set timeout to something like 10000ms in every .connect(), with no luck.
Once again, I have no problem connecting to Atlas with the mongo client (although it takes a few moments for the connection to be stablished), is just this test that fails every time.

.env:
SECRET_KEY=super_secret_key_you_should_change
MFLIX_DB_URI=mongodb+srv://m220student:m220password@cluster0-5qsjr.mongodb.net
MFLIX_NS=sample_mflix
PORT=5000

Error:
> server@1.0.0 test /mnt/data/Development/workspace/mflix-js
> jest --passWithNoTests “mongoclient”

Determining test suites to run...Setup Mongo Connection
 FAIL  test/lessons/mongoclient.spec.js
  ● Test suite failed to run

    TypeError: Cannot read property 'close' of undefined

      22 | 
      23 |   async teardown() {
    > 24 |     await this.global.mflixClient.close()
         |                                   ^
      25 |     await super.teardown()
      26 |   }
      27 | 

      at MongoEnvironment.teardown (test/config/mongoEnvironment.js:24:35)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        22.678s
Ran all test suites matching /mongoclient/i.
Teardown Mongo Connection
Jest did not exit one second after the test run has completed.

I tried to run the test in another machine and I get a different error:

> server@1.0.0 test /mnt/data/Desenvolvimento/workspace/mflix-js
> jest --passWithNoTests "mongoclient"

Determining test suites to run...Setup Mongo Connection
(node:22321) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to MongoClient.connect.
 FAIL  test/lessons/mongoclient.spec.js (12.622s)
  MongoClient
    ✓ Client initialized with URI (1968ms)
    ✓ Client initialized with URI and options (1766ms)
    ✓ Database handle created from MongoClient (2095ms)
    ✕ Collection handle created from database handle (2025ms)

  ● MongoClient › Collection handle created from database handle

    expect(received).toBeNull()

    Received: [MongoError: user is not allowed to do action [find] on [sample_mflix.movies]]

      135 |       expect(numMoves).toBe(23539)
      136 |     } catch (e) {
    > 137 |       expect(e).toBeNull()
          |                 ^
      138 |     } finally {
      139 |       testClient.close()
      140 |     }

      at toBeNull (test/lessons/mongoclient.spec.js:137:17)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
      at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:271:22)
      at Generator.prototype.(anonymous function) [as throw] (node_modules/regenerator-runtime/runtime.js:97:21)
      at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
      at _throw (node_modules/@babel/runtime/helpers/asyncToGenerator.js:29:9)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 3 passed, 4 total
Snapshots:   0 total
Time:        12.993s, estimated 16s
Ran all test suites matching /mongoclient/i.
Teardown Mongo Connection
Jest did not exit one second after the test run has completed.

Those are different Linux machines, where the version of the dependencies may differ (for example, in the first one I have the mongo client version 3.6 and in the second one version 4.2).

Update:
In this second machine, when I raise the connectTimeoutMS to, say, 20000, it works!

So, what could be wrong with the first one? There the error happens in the test/config/mongoEnvironment.js at the teardown() function. I’ ve seen posts here where people also have the same error, but I’m not sure what the solution is…

Many potential causes:

  1. You connect to the wrong cluster
  2. You connect with the wrong user
  3. The user has not been created with the appropriate privileges

I solved changing the config like this:
MFLIX_DB_URI=mongodb+srv://m220student:m220password@YOUR_SERVER_NAME_ADDRESS/?retryWrites=true&w=majority

YOUR_SERVER_NAME_ADDRESS is the plain ns like xxx.mongodb.net