2017-01-18 12 views
1

Settopending (f, fb) ist die erste aufgerufene Funktion, ich bin mir nicht sicher, ob ich die Rückrufe nicht korrekt geschrieben habe, weil applytransaction (t, f, fb) nie aufgerufen wird. "First" und "Second" werden gedruckt, aber "Third" und "Fourth" werden nicht gedruckt. Habe ich den Callback falsch eingerichtet, der applytransaction (t, f, fb) aufrufen soll oder gibt es da noch etwas anderes?Node.JS Callback-Funktion wird nicht ausgeführt

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

} 
+0

'Funktion update (Dokument, f, fb)' - erklärt es gibt keinen Rückruf Argument noch -auch genannt jeder Rückruf, 'f' und' fb' werden nie benutzt! ... akzeptiert 'this.transactions.update' einen Callback-Parameter? –

Antwort

1

als reine Vermutung, wenn this.transactions.update einen Rückruf von 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") 
} 

obwohl, wie f und fb sind nicht erforderlich, übernimmt

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

macht mehr Sinn

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

} 

Sie müssen den Callback-Parameter auch in dieser Funktion haben und dann callback() senden, wenn die Transaktion abgeschlossen ist.

1

Das Problem ist mit Ihrer Update-Methode. Sie rufen den FB nicht an (Callback-Methode). Nach dieser Operation "this.transactions.update" wird die Methode beendet, ohne dass die Callback-Funktion aufgerufen wird.

Fügen Sie danach einen Funktionsaufruf hinzu: fb (err, document);

Meistens würde "this.transactions.update" auch eine Callabck-Methode erwarten. Sie benötigen eine Logik gibt wie unten zu setzen:

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

}

+0

@Aaron Lassen Sie mich wissen, ob dies Ihr Problem gelöst hat. –