0

I have the following class:

const MongoClient = require("mongodb").MongoClient;
const ConnectionDetails = require("./ConnectionDetails").ConnectionDetails;
const Recipe = require("./recipe").Recipe;
var ObjectId = require('mongodb').ObjectID;

class MongoDriver{

    async #getClient(uriString){
        const client = new MongoClient(uriString);
        await client.connect();
        return client;
    }

    #getDatabase(client, dbString){
        const db = client.db(dbString);
        return db;
    }

    #getCollection(client, collectionString){
        return client.collection(collectionString)
    }
    
    static async addObject(object, connectionDetails){
        if(!connectionDetails instanceof ConnectionDetails){
            throw "connetionDetails must be of type ConnectionDetails but is " + typeof(connectionDetails);
        }

        if(!object instanceof Recipe){
            throw "object must be of type Recipe but is " + typeof(object);
        }
    
        const mongoClient = getClient(connectionDetails.getUri());
        const database = getDatabase(client, connectionDetails.getDatabase());
        const collection = getCollection(client, connectionDetails.getCollection());
        return await collection.insertOne(object)

    }

  }

  module.exports.MongoDriver = MongoDriver;

And when I call the addObject static method I'm getting the following error: ReferenceError: getClient is not defined

Why am I getting an undefined error when the function is defined in my class?

4
  • Try with this.getClient(... Commented Nov 28, 2020 at 21:15
  • 2
    Well there is no variable getClient. There is a this.#getClient private method that you could call. Commented Nov 28, 2020 at 21:16
  • Also change module.exports.MongoDriver = MongoDriver; to module.exports = MongoDriver;. Commented Nov 28, 2020 at 21:17
  • 1
    Really this should not be a class at all. There's no state, you never use an instance for anything (it would be empty). Export an async function addObject, and make those helper "methods" to local functions in the module. Commented Nov 28, 2020 at 21:18

1 Answer 1

1

Why am I getting an undefined error when the function is defined in my class?

That's the problem - you defined a class method, not a function. It would have to be called as this.#getClient (if it was static as well).

Really this should not be a class at all. There's no state, you never use an instance for anything (it would be empty). Export an async function addObject, and make those helper "methods" to local functions in the module:

const MongoClient = require("mongodb").MongoClient;
const ConnectionDetails = require("./ConnectionDetails").ConnectionDetails;
const Recipe = require("./recipe").Recipe;
var ObjectId = require('mongodb').ObjectID;

async function getClient(uriString){
    const client = new MongoClient(uriString);
    await client.connect();
    return client;
}

function getDatabase(client, dbString){
    const db = client.db(dbString);
    return db;
}

function getCollection(client, collectionString){
    return client.collection(collectionString)
}

exports.addObject = async function (object, connectionDetails){
    if(!connectionDetails instanceof ConnectionDetails){
        throw "connetionDetails must be of type ConnectionDetails but is " + typeof(connectionDetails);
    }

    if(!object instanceof Recipe){
        throw "object must be of type Recipe but is " + typeof(object);
    }

    const mongoClient = getClient(connectionDetails.getUri());
    const database = getDatabase(client, connectionDetails.getDatabase());
    const collection = getCollection(client, connectionDetails.getCollection());
    return await collection.insertOne(object);
}
Sign up to request clarification or add additional context in comments.

Comments

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.