2017-02-04 8 views
0

でオブジェクトが未定義と呼ばれていますが、オブジェクトtが正常に印刷された状態で「最初」が印刷されますが、そのコールバック後にコールバックが入力されます。 applied(t)関数では、エラー-TypeError:未定義のプロパティ '_id'を読み取ることができません。なぜなら、オブジェクトtは何らかの理由で未定義であるからです。このコールバック関数に入る前に未定義でなかった場合、何が原因である可能性がありますか? update()はMongoDB関数です。内部ネストされたコールバック関数

function applied(t) 
{ 
    this.transactions.update(
    { 
     _id: t._id, state: "pending" }, 
    { 
     $set: { state: "applied" }, 
     $currentDate: { lastModified: true } 
    } 
) 
} 

function applytransaction(t,f,fb) 
{ 

    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 } } 
    , function(err,t,y) { 
     console.log("First "+t); 
     this.model.update(
      { _id: t.destination, pendingTransactions: { $ne: t._id } }, 
      { $inc: { bal: y }, $push: { pendingTransactions: t._id } } 
     , function(err, t) { 
      console.log("Second " +t); 
      applied(t); 
     }); 

    }); 


} 
+0

「t」という名前の変数が多すぎます。それらに一意の名前を付けます。そのためには、1文字と2文字の変数名の使用をやめ、すべての変数に意味のある名前を付けてください。 – JLRishe

+0

2回目の更新が失敗するので、明らかに 't'はそれが – adeneo

答えて

0

Object t is for some reason undefined at this point. What could be the cause of this if it was not undefined prior to entering this Callback function? update() is a MongoDB function.

原因はあなたの最初のコールバックの内側t(2つ目)が最初t異なっているということです。それらに固有の名前を付け、エラーをチェックして、問題の内容を調べる必要があります。

あなたのコメントに基づいて更新:元のtを使用したい場合は、それを使用してください。 according to the docs、コールバックに渡される2番目の値は更新されたレコードの数であり、tではないため、何らかの理由でコールバックパラメータが出てくるとは思わないでください。

function applytransaction(t, f, fb) 
{ 
    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 } }, 
     function(err1, count1, status1) { 
      console.log("First err:", err1); 
      console.log("First ", t); 

      this.model.update(
       { _id: t.destination, pendingTransactions: { $ne: t._id } }, 
       { $inc: { bal: y }, $push: { pendingTransactions: t._id } }, 
       function(err2, count2) { 
        console.log("Second err", err2); 
        console.log("Second ", t); 

        applied(t); 
       } 
      ); 
     } 
    ); 
} 
+0

と思われません.2番目のコールバック関数のパラメータとしてObject tを使用しようとしています。パラメータt2を作成すると、tから必要なデータが得られません。 –

+0

@AaronReich私の答えを更新しました。 – JLRishe

関連する問題