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