UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: db.collection is not a function

1 views
0

I am having problem in inserting an array object in mongo:

var MongoClient = require('../node_modules/mongodb').MongoClient;
var dbName = 'googleImages';
var dbObj;

module.exports = {
    connectDB: function(){

        dbObj={};        
        var url = "mongodb://localhost:27017/"+dbName;

        MongoClient.connect(url, function(err, db) {

          if (err)
            {
                console.log('error');
                return null;
            }

          console.log("Database created/connected!");
          //
          db.collection('temp').insert({name: 'sachin'});
          //db.collection.insert();
          dbObj=db; 
        }); 
    return dbObj;
    }  
}

below is the function in external file which is calling the above module:

const mongo = require(‘../LIB/mongoModule’);

function insertImageDetails(){
//file Details insertion
var db = mongo.connectDB();
if(db === null)
return;
}

since everything is fine I am getting this error: (node:976) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: db.collection is not a function
please Help ASAP