Node.JS回调函数未被执行

时间:2017-01-18 02:31:41

标签: javascript node.js mongodb callback

settopending(f,fb)是第一个调用的函数,我不确定我是否没有正确编写回调函数,因为从不调用applytransaction(t,f,fb)。 "首先"和"第二"印刷但是"第三"和"第四"不打印。我是否错误地设置了应该调用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 } }
)

}

3 个答案:

答案 0 :(得分:1)

纯粹猜测,如果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);
        });
    });
}

更有意义

答案 1 :(得分:1)

问题在于您的更新方法。你没有调用fb(任何地方的回调方法)。在此操作" this.transactions.update"之后,该方法将在不调用传递的回调函数的情况下完成。

之后添加一个函数调用: FB(ERR,文件);

function update(document,f,fb)
{

this.transactions.update(
    { _id: document._id, state: "initial" },
    {
        $set: {state: "pending"},
        $currentDate: {lastModified: true}
    }

);
console.log("Second")

}

主要是这个" this.transactions.update"也期待一个callabck方法。你需要把你的逻辑放在下面:

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

}

答案 2 :(得分: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")

}

您还需要在此函数中使用callback参数,然后在事务完成时发送callback()。