settopending(f,fb) 是第一个调用的函数,我不确定我是否没有正确编写回调,因为从未调用过 applytransaction(t,f,fb)。打印“First”和“Second”,但不打印“Third”和“Fourth”。我是否错误地设置了应该调用 applytransaction(t,f,fb) 的回调,还是有其他问题?
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 } }
)
}
最佳答案
纯属猜测,如果 this.transactions.update
接受回调
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")
}
尽管 f
和 fb
不是更新所必需的
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);
});
});
}
更有意义
关于javascript - Node.JS 回调函数未被执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41710447/