嗯,这应该有效,一切(当经历它时)一步一步有效。除了实际写入数据库之外。在我的 sails 应用程序中,我有一个通往以下功能的路线:
add: function (req, res) {
let params = req.allParams();
let idx = params.event_id;
let name = params.name;
let sport = params.sport;
let location = params.location;
let idNr = Number(idx);
let sportNr = Number(sport);
let locationNr = Number(location);
let dat = {
name:name,
id:idNr,
sport:sportNr,
location:locationNr
};
console.log(dat);
Event.create(dat)
.then(function (res){ return res.ok(); })
.catch(function (err){
console.log(err);
return res.send(err);
});
}
这确实是一个简单的函数,对吗?然而,在我的“良好”数据上,它失败了:
{
"error": "E_VALIDATION",
"status": 400,
"summary": "2 attributes are invalid",
"model": "Event",
"invalidAttributes": {
"location": [
{
"rule": "number",
"message": "Value should be a number (instead of \"3\", which is a string)"
}
],
"sport": [
{
"rule": "number",
"message": "Value should be a number (instead of \"2\", which is a string)"
}
]
}
}
怎么会发生这种事?我显然正在使用 dat
,它采用“运动”和“位置”的数字变体。 - 记录甚至显示数据已按预期填充了数值。
所以失败是没有意义的;不应该。
<小时/> 编辑,模型定义为:module.exports = {
attributes: {
id: {
type: 'number',
required: true,
unique: true,
},
name: {
type: 'string',
required: true
},
location: {
type: 'number',
required: true,
},
sport: {
type: 'number',
required: true
},
},
};
最佳答案
此问题似乎是 sails 支持的限制
type:'number'
来自官方sails documentation关于模型属性。支持以下属性类型:
- 字符串
- 文本
- 整数
- float
- 日期
- 日期时间
- bool 值
- 二进制
- 数组
- json
- 中文本
- 长文本
- 对象ID
作为使用 javascript 的替代方案
Number()
函数,你可以考虑使用
parseInt()
对于已知整数值或
parsefloat()
对于已知的 float 。
关于javascript - Sails ORM 不理解数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48171655/