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?
this.getClient(...getClient. There is athis.#getClientprivate method that you could call.classat all. There's no state, you never use an instance for anything (it would be empty). Export anasync function addObject, and make those helper "methods" to local functions in the module.