Javascript Sails.js beforeValidate()清空值参数
我遇到beforeValidate()问题,无法在线找到任何答案。我的模型有两个关系属性,需要在发布的数据中使用id号。如果用户发布字符串,则会出现验证错误。我想让用户能够发布字符串数据,然后在beforeValidate()中使用findOrCreate()查找或创建属性的相关模型,然后用相关模型的ID覆盖发布数据的属性 我有以下型号:Javascript Sails.js beforeValidate()清空值参数,javascript,sails.js,Javascript,Sails.js,我遇到beforeValidate()问题,无法在线找到任何答案。我的模型有两个关系属性,需要在发布的数据中使用id号。如果用户发布字符串,则会出现验证错误。我想让用户能够发布字符串数据,然后在beforeValidate()中使用findOrCreate()查找或创建属性的相关模型,然后用相关模型的ID覆盖发布数据的属性 我有以下型号: attributes: { reporter : { model: 'reporter', required: true }, loca
attributes: {
reporter : { model: 'reporter', required: true },
location : { model: 'location', required: true },
line : { type: 'boolean', required: true, defaultsTo: true },
count : { type: 'int', required: true, defaultsTo: 0},
composition : { type: 'int', required: true, defaultsTo: 3}
},
beforeValidate: function(values, next) {
if (typeof values.reporter !== 'number') {
Reporter.findOrCreate({name: values.reporter}).exec(function(data){
console.log(data)
values.reporter = data.id;
})
};
next();
},
我正在将此数据发布到模型的create()默认蓝图端点:
{
"location":"Harry's",
"reporter":"tim",
"count":30,
"composition":3,
"line":false
}
在beforeValidate()中记录上述值时,我得到以下结果:
{ location: NaN,
reporter: NaN,
count: '30',
composition: '3',
line: false }
当我用ID替换“位置”和“报告者”时,我没有得到任何错误。为什么beforeValidate()函数中的字符串值会被去掉?也许你需要先发布(创建)到端点位置模型,然后发布(创建)到报告器模型,然后发布YOUMODEL/id/reporterID,POST YOUMODEL/id/location id我认为这不是问题所在。当我发布整数时,无论该主键是否存在模型,当我记录值时,该属性仍然设置为整数。不过谢谢你的想法。内置类方法在世界上哪里有文档记录?(
在创建之前
,在验证之前
等)