Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

123
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda