【发布时间】:2015-01-03 19:19:14
【问题描述】:
在Sails.js 中,可以为模型使用不同的数据库连接,并且可以轻松更改数据库(MySQL 或 MongoDB)。当我想显示验证错误时,就会出现问题。这些是我的代码:
Groups.js 模型
...
connection: 'MysqlConnection', //or connection: 'MongodbConnection'
attributes: {
id: {
type: 'string',
unique: true
},
name: {
type: 'string',
required: true
},
...
GroupsController.js 控制器:
...
//add group to database
Groups.create(group, function (err, data) {
if (err) {
console.log(err);
res.send('Error'); // is it possible to send only validation error
return;
} else {
res.send(data);
}
});
...
我应该单独处理每个属性验证错误,是否可以只发送验证错误?
MySQL 返回:
Error (E_VALIDATION) :: 1 attribute is invalid ...
MongoDB 返回:
Error (E_UNKNOWN) :: Encountered an unexpected error ...
【问题讨论】:
-
我也有同样的问题。您可以制作 console.log(req.params.all());并显示出了什么问题。在我的例子中,id 是未定义的。
标签: javascript sails.js sails-mongo