我正在尝试使用 Mongoose 提供的验证,所以有一个方案
var MeecaptureStackTracetingSchema = new Schema({
title: {type: String, required : true}
....
尝试在不设置标题的情况下保存 - 到了下面的点,Mongoose 想要返回缺少字段的错误,但失败了“没有方法'captureStackTrace'”
/mongoose/lib/error.js
function MongooseError (msg) { // Validator "required" failed for path title with value `undefined`
Error.call(this);
Error.captureStackTrace(this, arguments.callee); // Falls over here
this.message = msg;
this.name = 'MongooseError';
};
抛出此错误
TypeError: Object function model(doc, fields, skipId) {
if (!(this instanceof model))
return new model(doc, fields, skipId);
Model.call(this, doc, fields, skipId);
} has no method 'captureStackTrace'
我设置了一个非常基本的测试
function MyError(msg){
this.name = 'MyError';
this.message = msg;
Error.call(this, msg);
Error.captureStackTrace(this, this.constructor);
}
MyError('New error')
效果很好。那么有什么办法可以在我的代码集中混合 captureStackTrace
吗?我查了一下,没有发现任何明显的名字误用。
最佳答案
在 Mongoose 模型中,我声明了一个全局变量 Error,这与 Node.js 的原生 Error 实例发生了冲突。将其重命名为 LogError 有帮助。
关于Node.js v0.10.18 Mongoose 验证失败于方法 'captureStackTrace' ”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22404912/