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

200
Views
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 answers
Answer question

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 Report

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 Report

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 Report
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!