0

I have database.js where my Database class is.

database.js

class Database {
    constructor() {
        this.uri = "mongodb+srv: ....."
    }
    get_uri = () => {
        return this.uri
    }
    connect = async client => {
        await client.connect();
        console.log("Successfully connected!")
        return client;
    }

    disconnect = async client => {
        await client.close();
    }
}

exports.Database = new Database()

server.js

const {MongoClient} = require('mongodb');
const db = require("./server/database");
const connect = db.connect(); // TypeError: db.connect is not a function

files

server -|
        |-database.js

package.json
server.js

printed db variable for my code above

[0] Database object:
[0] {
[0]   Database: Database {
[0]     get_uri: [Function: get_uri],
[0]     connect: [AsyncFunction: connect],
[0]     disconnect: [AsyncFunction: disconnect],
[0]     uri: 'mongodb+srv:......'
[0]   }
[0] }

EDIT 1

Soulka, just done this:

const {MongoClient} = require('mongodb');
const { Database } = require("./server/database");
const connect = Database.connect();

and still TypeError: Cannot read property 'connect' of undefined

But it raises an error, which I put as a comment above. What I am doing wrong?

Thank you!

3
  • your file ist Database.js not database.js. use const db = require("./server/Database"); Also your db.connect() is an async function. You might want to await it. Commented Feb 4, 2021 at 9:00
  • @SoulKa oh, it's database.js, edited! Commented Feb 4, 2021 at 9:01
  • can you try to print out the db object in server.js? Commented Feb 4, 2021 at 9:02

1 Answer 1

1

Your import should be const { Database } = require("./server/database"); instead of const db = require("./server/database");

This is because you are exporting via exports.Database = .. not exports = ...

EDIT (for your edit): I'm not sure if it makes a difference, but you should think about using a singleton like this:

class Database {
    constructor() {
        this.uri = "mongodb+srv: ....."
    }

    static get instance() {
        if (!Database._instance) Database._instance = new Database();
        return Database._instance;
    }

    get_uri = () => {
        return this.uri
    }
    connect = async client => {
        await client.connect();
        console.log("Successfully connected!")
        return client;
    }

    disconnect = async client => {
        await client.close();
    }
}

exports.Database = Database

And then in server.js

const {MongoClient} = require('mongodb');
const { Database }= require("./server/database");
const db = Database.instance;
const connect = db.connect( someClientMustGoHere ); // <---- THIS LINE

EDIT 2: Fixed the last line of server.js to be const connect = db.connect( someClientMustGoHere );

Sign up to request clarification or add additional context in comments.

2 Comments

About bold edit - still ` Cannot read property 'connect' of undefined, and printed db variable` is same
you are forgetting to pass the client to the connect( client ) function! It must be db.connect( someClient ); Maybe assign the client to the Database instance so it can be accessed via this.client?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.