This is my code, I had tried to set class value to db.db('mydb') to using in other function below
class Database {
constructor() {
this.db_url = "mongodb://127.0.0.1:27017/";
let mongodb = require("mongodb").MongoClient;
mongodb.connect(this.db_url, { useUnifiedTopology: true }, function(err, db) {
if (err) throw err;
this.dbo = db.db('mydb');
})
}
}
I tried to set dbo value of class = db.db('mydb') but I recieved this error
this.dbo = db.db('mydb')
^
TypeError: Cannot set property 'dbo' of undefined
but when i append console.log(db.db('mydb'))
it still print out normally