【问题标题】:Node.JS Callback function not being executedNode.JS 回调函数没有被执行
【发布时间】:2017-01-18 02:31:41
【问题描述】:

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

}

【问题讨论】:

  • function update(document,f,fb) - 没有声明回调参数,也没有调用任何回调 - 而且,ffb 甚至从未使用过! ...this.transactions.update 是否接受回调参数?

标签: javascript node.js mongodb callback


【解决方案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);
        });
    });
}

更有意义

【讨论】:

    【解决方案2】:

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

    之后添加一个函数调用: fb(错误,文档);

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

    }

    【讨论】:

      【解决方案3】:
      function update(document,f,fb, callback)
      {
      
      this.transactions.update(
          { _id: document._id, state: "initial" },
          {
              $set: {state: "pending"},
              $currentDate: {lastModified: true}
          },
          callback();
      
      );
      console.log("Second")
      
      }
      

      你需要在这个函数中也有回调参数,然后在事务完成时发送回调()。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-12
        • 1970-01-01
        • 1970-01-01
        • 2013-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多