I have a CustomStore class, in its constructor I connect to a Mongo table. In class I have a saveToken()
function that saves data in a table.
class CustomStore extends TokenStore {
constructor(){
connectDb(function(){
console.log("Connected to db")
});
}
This is the connectDb()
function called in the constructor
function connectDb(callback){
let url = 'mongodb://localhost/zoho_whatsapp';
let dbName = 'zoho_auth';
let client = new MongoClient(url, { useNewUrlParser: true });
client.connect((err) => {
if (err) { throw err; }
let db = client.db(dbName);
let collection = db.collection('oauthtokens');
callback(collection, client, db);
});
}
And this is the saveToken()
function
saveToken(user, token) {
console.log("The token is saved");
}
}
Now I want to call the function from another file
new zohoPersists.CustomStore().saveToken (user, token);
But the function ran before the constructor and I have not yet connected to the Mongo table. Do I need to write a promise function? I do not know enough ...
Thanks