Client.Close() is not a function?
==> My code
(node:29533) UnhandledPromiseRejectionWarning: TypeError: client.close is not a function
I get this error if I do

const db = new KeyMongo(`mongodb+srv://`,{db:"mongo-test",collection:"keyv"})

Hi @Jitesh_D,

You need to call MongoClient.connect() to establish a connection. This method either returns a promise or execute a callback. Depending on how you would like to use KeyMongo class, there are many ways to incorporate connect() in the class.

One way is have another async method to initialise KeyMongo . For example:

    async init(callback){
        this.client = new MongoClient(this.uri, {useNewUrlParser: true, useUnifiedTopology: true })
        await this.client.connect(); 

    async set(key, value){
        let result = await createListing(this, key, value)

    async function createListing(obj, key, value){
        try {
            insert = await obj.client.db(obj.db).collection(obj.collection).insertOne({"_id": key,"value":value})
            return insert
        } finally {
            await obj.client.close()

Then you can invoke KeyMongo using:

const km = new KeyMongo(`MONGODB_URI`, {db:"dbName",collection:"collName"})

Thank you for providing the code snippet that’s quite useful. If you have further questions, please provide which MongoDB Node.JS driver you are using.