Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
Calls a function within the class before calling the constructor

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!