1

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 } }
)

}
1
  • function update(document,f,fb) - there's no callback argument declared nor any callback called -also, f and fb are never even used! ... does this.transactions.update accept a callback parameter? Commented Jan 18, 2017 at 2:52

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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")

}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.