javascript - Node.JS 回调函数未被执行

标签 javascript node.js mongodb callback

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

尽管 ffb 不是更新所必需的

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/

相关文章:

javascript - 崩溃后,即使我回滚到工作版本, meteor 应用程序也无法工作?

javascript - 在 handlebars.js 中使用具有数字属性的对象

javascript - Mongoose 枚举数

javascript - 从 div 内的 p 标签中选择一个值

javascript - Sinon spy.called 没有工作

node.js - 如何使用mongoDb获取字符串中关键字的出现次数

node.js - 如何在 Nodejs 中将字符串作为读取流对象传递?

c# - mongodb 4.0 在c#(驱动版本2.7+)中启动一个事务,在事务期间,整个数据库将被锁定,如何解决?

mongodb - 级联:'all-delete-orphan'如何工作?

javascript - 在日期选择器中选择多个日期