2012-04-04 11 views
1

ので、私はここで、接続・モンゴまたはexpressjsによって引き起こされる結合の問題は、コードされていると思うものに実行しています:Expressjでバインディングロスが発生する可能性はありますか?

//Error 
    app.use(function(err, req, res, next) { 

     if (err instanceof noData) { 
      res.send(err, 404); 
     } else { 
      next(err); 
     } 
    }); 

マイカスタムエラーハンドラここでエラーを投げる

function noData(err){ 
    this.code = 0; 
    this.msg = err; 
    console.log(Error); 
    Error.call(this, {code:0, msg:err}); 
    Error.captureStackTrace(this, arguments.callee); 
}; 
noData.prototype.__proto__ = Error.prototype;  

err = true; 
    //if(err) throw new noData('No Password'); 
    //Get user from database 
    db.collection('users').find({}, {limit:1}).toArray(function(err, result) { 
    if(err) throw new noData('No Data');    
    }); 

最初のエラーは正しくスローされますが、2番目のエラーは正常にスローされますが、2番目のエラーは一般的なnodejsエラーをスローします。

throw e; // process.nextTick error, or 'error' event on first tick 

私がここで間違って何をしているのですか? connect-mongoは何とかバインディングを失いますか? ご意見をいただければ幸いです。

答えて

0

問題がexpressまたはconnect-mongoにない場合、コールバックは異なるスコープにあります。これを解決するには、コールの最後に(this)を追加するだけです。

//Get user from database 
    db.collection('users').find({}, {limit:1}).toArray(function(err, result) { 
    if(err) throw new noData('No Data');    
    }(this)); 

ノードが私のカスタムエラーについて知っています。 (hallelujah) これは本質的にiifeで、私はparamで渡すことができます。