【发布时间】:2015-04-04 07:15:28
【问题描述】:
我正在使用sails-mongo 适配器在mongodb 中试用sailsJs。 向模型添加验证后,当验证失败时,我会收到以下响应。
Users.js 模型:
module.exports = {
schema: true,
attributes: {
name: {
type: "string",
unique: true
},
email: {
type: "email",
unique: true
},
password: {
type: "string",
required: true
}
}
}
使用sails-mongo 适配器时出现验证错误:
{
"error": {
"error": "E_UNKNOWN",
"status": 500,
"summary": "Encountered an unexpected error",
"raw": {
"name": "MongoError",
"code": 11000,
"err": "E11000 duplicate key error index: eReporterDB.users.$name_1 dup key: { : \"codin\" }"
}
}
}
如果我使用作为sails-disk适配器的开发数据库,我会得到更好的格式化响应。
使用sails-disk 适配器时出现验证错误:
{
"error": {
"error": "E_VALIDATION",
"status": 400,
"summary": "2 attributes are invalid",
"invalidAttributes": {
"name": [
{
"value": "codin",
"rule": "unique",
"message": "A record with that `name` already exists (`codin`)."
}
]
}
}
}
作为一名开发人员,我希望从框架中得到标准化的响应,
任何人都可以帮助我以一种优雅的方式处理此类验证错误。
我的意思是我不能只向外行用户显示错误"E11000 duplicate key error index: eReporterDB.users.$name_1 dup key: { : \"codin\" }"。
谢谢。
【问题讨论】:
-
你需要发布你的模型
标签: javascript node.js mongodb sails.js sails-mongo