我是 MEAN 堆栈的新手,所以请原谅我幼稚的问题,但我无法弄清楚如何将自定义错误消息从我的 Mongoose 预保存钩子中间件传递到我的 Angularjs 控制器。
我正在使用预保存钩子来检查以前是否保存过文档。如果有,那么我想取消保存尝试,而是更新文档。我使用了next(new Error('Error message'))
方法来防止save
操作。下面是我的代码:在我的后端:
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);
}
);
换句话说,如何将自定义对象从猫鼬中间件(预保存钩子(传递给 Angular 控制器?
我在项目中所做的是,我创建了一个错误.js文件,并且还创建了许多可能的错误,当需要发送时,我只需要我的错误.js并发送错误,如以下代码:
错误.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
}
如果返回 next(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"
}
**据我了解,您希望将数据传递给下一个中间件。然后你可以在当前的中间件中执行此操作:
res.locals.result={ X:1,Y:1};
在下一个中间件中,您将能够获得这样的 X,Y:console.log(res.locals.result);
您可以使用res.locals
在下一个中间件中传递任何内容。