当我调用validateSync()时,如何删除ValidationError中的堆栈跟踪:
user.validateSync();
console.log(user.errors);
我得到:
email:
{ ValidatorError: Path `email` is required.
at MongooseError.ValidatorError (e:\NodeJS\ProjectDemo\Socket\node_modules\mongoose\lib\error\validator.js:24:11)
at validate (e:\NodeJS\ProjectDemo\Socket\node_modules\mongoose\lib\schematype.js:783:13)
at e:\NodeJS\ProjectDemo\Socket\node_modules\mongoose\lib\schematype.js:818:9
at Array.forEach (native)
at SchemaString.SchemaType.doValidateSync (e:\NodeJS\ProjectDemo\Socket\node_modules\mongoose\lib\schematype.js:796:14)
at e:\NodeJS\ProjectDemo\Socket\node_modules\mongoose\lib\document.js:1479:17
at Array.forEach (native)
at model.Document.validateSync (e:\NodeJS\ProjectDemo\Socket\node_modules\mongoose\lib\document.js:1463:9)
at e:\NodeJS\ProjectDemo\Socket\routes\users.js:19:20
at Layer.handle [as handle_request] (e:\NodeJS\ProjectDemo\Socket\node_modules\express\lib\router\layer.js:95:5)
at next (e:\NodeJS\ProjectDemo\Socket\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (e:\NodeJS\ProjectDemo\Socket\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (e:\NodeJS\ProjectDemo\Socket\node_modules\express\lib\router\layer.js:95:5)
我不希望堆栈跟踪验证错误。请帮帮我。
答案 0 :(得分:0)
您需要将NODE_ENV设置为生产才能禁用stacktrace,您可以将其更改为环境变量
set NODE_ENV=production
或者如果不方便,请按照以下说明在.js本身中执行。
process.env.NODE_ENV = 'production';