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

202
Vistas
Node.JS Callback function not being executed

settopending(f,fb) is the first function called, I am not sure if I did not write the callbacks correctly because applytransaction(t,f,fb) is never called. "First" and "Second" is printed but "Third" and "Fourth" are not printed. Did I incorrectly set up the callback that is supposed to call applytransaction(t,f,fb) or is there something else that is the problem?

function update(document,f,fb)
{

this.transactions.update(
    { _id: document._id, state: "initial" },
    {
        $set: {state: "pending"},
        $currentDate: {lastModified: true}
    }

);
console.log("Second")

}


function settopending(f,fb)
{
console.log("First");

var t = this.transactions.findOne( { state: "initial" } , function(err, document) {//CALLBACK

    update(document,f,fb , function(err, document) {//CALLBACK
         console.log("Third");
        applytransaction(document,f,fb);

    });

});


}


function applytransaction(t,f,fb)
{
console.log("Fourth");
x=fb(t.value);
y=f(t.value);

this.model.update(
    { _id: t.source, pendingTransactions: { $ne: t._id } },
    { $inc: { bal:x }, $push: { pendingTransactions: t._id } }
);
   this.model.update(
    { _id: t.destination, pendingTransactions: { $ne: t._id } },
    { $inc: { bal: y }, $push: { pendingTransactions: t._id } }
)

}
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

as a pure guess, if this.transactions.update accepts a callback

function update(document, f, fb, cb) { // added cb parameter
    this.transactions.update({ _id: document._id, state: "initial" },
        {
            $set: {state: "pending"},
            $currentDate: {lastModified: true}
        }, cb // added cb argument
    );
    console.log("Second")
}

although, as f and fb are not required by update

function update(document, cb) {
    this.transactions.update({ _id: document._id, state: "initial" },
        {
            $set: {state: "pending"},
            $currentDate: {lastModified: true}
        }, cb
    );
    console.log("Second")
}

function settopending(f,fb) {
    console.log("First");
    var t = this.transactions.findOne( { state: "initial" } , function(err, document) {//CALLBACK
        update(document, function(err, document) {//CALLBACK
            console.log("Third");
            applytransaction(document,f,fb);
        });
    });
}

makes more sense

over 4 years ago · Santiago Trujillo Denunciar

0

The issue is with your update method. You are not calling the fb(callback method anywhere). after this operation "this.transactions.update", the method is getting over without calling calling the passed callback function.

add a function call after that: fb(err,document);

function update(document,f,fb)
{

this.transactions.update(
    { _id: document._id, state: "initial" },
    {
        $set: {state: "pending"},
        $currentDate: {lastModified: true}
    }

);
console.log("Second")

}

Mostly this "this.transactions.update" would be expecting a callabck method too. You need to put your logic there like below:

function update(document,f,fb){    
    this.transactions.update(
        { _id: document._id, state: "initial" },
        {
            $set: {state: "pending"},
            $currentDate: {lastModified: true}
        },function(err,doc){
            if(err){
                fb(err,null)
            }else{
                fb(null,doc)
            }

        }

    );
    console.log("Second")

}

over 4 years ago · Santiago Trujillo Denunciar

0

function update(document,f,fb, callback)
{

this.transactions.update(
    { _id: document._id, state: "initial" },
    {
        $set: {state: "pending"},
        $currentDate: {lastModified: true}
    },
    callback();

);
console.log("Second")

}

You need to have the callback parameter in this function as well and then send callback() when the transaction is done.

over 4 years ago · Santiago Trujillo Denunciar
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