2017-07-12 8 views
1

私は平均的なスタックに新しいですので、私の素朴な質問を赦してくださいが、私はMongooseからカスタムエラーメッセージを渡す方法を理解できませんでした私のAngularjsコントローラにフックミドルウェアをあらかじめ保存する。

私は以前に保存された文書かどうかを確認するために保存前フックを使用しています。それがある場合は、私は保存しようとするのをキャンセルし、ドキュメントを更新したいと思います。私はsaveアクションを防ぐためにnext(new Error('Error message'))メソッドを使用しました。以下は私のコードです:

私のバックエンドでは:私のクライアントのコントローラでmongooseミドルウェアnext()メソッドに渡されたエラーメッセージをクライアントコントローラ

VoteSchema.pre('save',function(next){ 
    var self = this; 
    self.constructor.findOneAndUpdate(
    {'choice._id':self.choice._id}, 
    {$inc:{'choice.count':1}}, 
    {new:true}, 
    function(err,doc){ 
     if(err) return next(err); 
     if(doc!==null){ 

     //I want to be able to pass a type like this {x:1, y:2} 
     //to my angular controller from here. How do I do this? 

     return next(new Error("Canceled save attempt!")); //this cancels the save 
     } 
     return next(); 
    }); 
}); 

:つまり

vote.$save(function (response) { 
     console.log(response); 
    }, 
    function (errorResponse) { 

     //I want to get my custom type from the pre-save 
     //middleware hook here which was equals to {x:1, y:2} 
     //or at least the message "Canceled save attempt!" 
     //Note that I checked the 'errorResponse' object but 
     //it isn't anywhere there 

     console.log(errorResponse); 
    } 
); 

、どのように私はマングースミドルウェアからカスタムオブジェクトを渡すことができます(プリセーブフック)を角型コントローラに接続しますか?私は私のプロジェクトでやっている何を

答えて

0

、私はerror.jsファイルを作成しても、多くの可能性のあるエラーを作成している、そして私は単に私のerror.jsを必要とし、コードの下のようなエラーを送信する送信するために必要とされています

error.js:

var errors = {}; 
var util = require('util'); 

/** 
* Used to create errors 
* @Param {Number} httpCode - http response code 
* @Param {String} message - Error message 
*/ 
function ApiError(httpCode, httpMessage, description) { 
    this.httpCode = httpCode; 
    this.httpMessage = httpMessage; 
    this.description = description; 
    this.details = null; 
} 

// inherit the Error class 
util.inherits(ApiError, Error); 

// exporting error Object 
module.exports = errors; 

errors.ApiError = ApiError; 

/** 
* Sends httpCode and message to user 
* @Param {Response} res - http response 
*/ 
ApiError.prototype.send = function (res) { 
    res.status(this.httpCode); 
    res.json({ httpCode: this.httpCode, httpMessage: this.httpMessage, description: this.description, details: this.details }) 
} 

ApiError.prototype.withDetails = function (details) { 
    this.details = details; 
    return this; 
}; 

errors.not_registeration = new ApiError(404, 'NOT_REGISTERATION', 
    'User not registered with this email/mobile.'); 


errors.invalid_login = new ApiError(400, 'INVALID_LOGIN', 
    'Login credentials do not match any registered user.'); 

あなたは私が既に作成しているように、ここでのコードを使用して独自のエラーを作成することができます。

私のファイル内のエラーを使用する方法

var errors = require('../../library/errors'); 
..... 
..... 
..... 
VoteSchema.pre('save',function(next){ 
    var self = this; 
    self.constructor.findOneAndUpdate(
    {'choice._id':self.choice._id}, 
    {$inc:{'choice.count':1}}, 
    {new:true}, 
    function(err,doc){ 
     if(err) return next(err); 
     if(doc!==null){ 

     return next(errors.not_registeration) // Here i am sending errors. 
     or 
     return next(errors.invalid_input.withDetails("This is extra details")); 

     } 
     return next(); 
    }); 
}); 

応答が毎回のようになります。

もしリターン次の(errors.not_registeration) -

{ 
     "httpCode": 404, 
     "httpMessage": "NOT_REGISTERATION", 
     "description": "User not registered with this email/mobile.", 
     "details": null 
    } 

ret骨壷次の(errors.invalid_input.withDetails(「これは、余分な詳細である」)) -

{ 
    "httpCode": 400, 
    "httpMessage": "INVALID_INPUT", 
    "description": "The request input is not as expected by API. Please provide valid input.", 
    "details": "This is extra details" 
} 

** 私はあなたが次のミドルウェアにデータを渡したい理解しています。 そして、あなたは現在のミドルウェアでこれを行うことができます:あなたがそのようにX、Yを得ることができるだろう次のミドルウェアで

res.locals.result={ X:1,Y:1}; 

を:console.log(res.locals.result); あなたはres.localsを使って、次のミドルウェアに何かを渡すことができます。

+0

クライアントコントローラ –

+0

の 'next()'関数に渡されたエラーメッセージが表示されないので、res.locals.result = {X:1、Y:1 }、次のミドルウェアではX、Yを得ることができます。次のミドルウェアconsole.log(res.locals.result); –

+0

私は私の答えも更新しました。見て、それが有用かどうか私に知らせてください。 –

関連する問題