【发布时间】:2019-06-13 03:31:43
【问题描述】:
我正在使用mongoose-unique-validator 插件来验证我的架构中的用户名,并且我正在使用 Postman 来测试我的路线。当我使用我知道已经注册的用户名测试我的应用程序的注册路由时,Postman 将挂断 POST 请求,但我的控制台中会出现猫鼬验证错误。我的问题是,如何让 Postman 将 mongoose 验证错误消息显示为 JSON,以便我可以在我的应用程序中使用该错误消息?
const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {
type: String,
required: true
},
username: {
type: String,
required: true,
unique: true,
uniqueCaseInsensitive: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now()
}
});
UserSchema.plugin(uniqueValidator, { message: 'Someone already has that username'});
module.exports = User = mongoose.model('users', UserSchema);
【问题讨论】:
标签: mongodb error-handling postman mongoose-schema