私は平均的なスタックに新しいですので、私の素朴な質問を赦してくださいが、私は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);
}
);
、どのように私はマングースミドルウェアからカスタムオブジェクトを渡すことができます(プリセーブフック)を角型コントローラに接続しますか?私は私のプロジェクトでやっている何を
クライアントコントローラ –
の 'next()'関数に渡されたエラーメッセージが表示されないので、res.locals.result = {X:1、Y:1 }、次のミドルウェアではX、Yを得ることができます。次のミドルウェアconsole.log(res.locals.result); –
私は私の答えも更新しました。見て、それが有用かどうか私に知らせてください。 –