Waterline error: "Unknown rule: default"
Waterline error: "Unknown rule: default"
在模型上调用 .create()
时出现以下错误。
型号:
attributes: {
user : {
type: 'integer'
},
number : {
type: 'string'
}
}
通话:
sails.models.phone.create({
user: 2,
number: '5555555555',
updated_at: Sun Nov 27 2016 16:59:45 GMT-0800 (PST)
});
错误:
Unknown rule: default
at Object.matchRule (/Developer/repos/bond/api/node_modules/waterline/node_modules/anchor/lib/match/matchRule.js:38:11)
at Anchor.to (/Developer/repos/bond/api/node_modules/waterline/node_modules/anchor/index.js:106:48)
at afterwards (/Developer/repos/bond/api/node_modules/waterline/lib/waterline/core/validations.js:229:41)
at /Developer/repos/bond/api/node_modules/async/lib/async.js:52:16
at Object.async.forEachOf.async.eachOf (/Developer/repos/bond/api/node_modules/async/lib/async.js:236:30)
at Object.async.forEach.async.each (/Developer/repos/bond/api/node_modules/async/lib/async.js:209:22)
at _eachValidation (/Developer/repos/bond/api/node_modules/waterline/lib/waterline/core/validations.js:189:11)
at /Developer/repos/bond/api/node_modules/async/lib/async.js:181:20
at Object.async.forEachOf.async.eachOf (/Developer/repos/bond/api/node_modules/async/lib/async.js:233:13)
at Object.async.forEach.async.each (/Developer/repos/bond/api/node_modules/async/lib/async.js:209:22)
at Validator.validate (/Developer/repos/bond/api/node_modules/waterline/lib/waterline/core/validations.js:144:9)
at /Developer/repos/bond/api/node_modules/waterline/lib/waterline/query/validate.js:42:25
at /Developer/repos/bond/api/node_modules/async/lib/async.js:718:13
at Immediate.iterate [as _onImmediate] (/Developer/repos/bond/api/node_modules/async/lib/async.js:262:13)
at processImmediate [as _immediateCallback] (timers.js:383:17)
我该如何解决这个问题?
我查看了 similar errors,但我的 none 个模型具有 default
属性,包括上面显示的 phone
模型。为什么会出现这种情况,我该如何解决?
解决方案
所以结果是 .update()
的 Waterline 调用改变了输入信息。我写了一个函数来创建或更新水线模型,当他们修复 auto updated_at issue 时,他们一定改变了传入的信息。
我以前的代码不起作用:
createOrUpdate: function(model, primary, data){
return model.update(primary, data)
.then(function updateCB(updated){
if (updated.length == 0){
return model.create(data) //data here had an added updated_at field
.then(function(created){
return created;
});
}
if (updated.length > 0){
updated = updated[0];
}
return updated;
});
}
我的新代码有效:
createOrUpdate: function(model, primary, data){
const data1 = JSON.parse(JSON.stringify(data)); // deep copy
const data2 = JSON.parse(JSON.stringify(data)); // deep copy
return model.update(primary, data1)
.then(function updateCB(updated){
if (updated.length == 0){
return model.create(data2)
.then(function(created){
return created;
});
}
if (updated.length > 0){
updated = updated[0];
}
return updated;
});
}
你不能调用更新:
updated_at: Sun Nov 27 2016 16:59:45 GMT-0800 (PST)
Updated_at是自动填充的字段。从请求中删除它(随叫随到)它会起作用。
在模型上调用 .create()
时出现以下错误。
型号:
attributes: {
user : {
type: 'integer'
},
number : {
type: 'string'
}
}
通话:
sails.models.phone.create({
user: 2,
number: '5555555555',
updated_at: Sun Nov 27 2016 16:59:45 GMT-0800 (PST)
});
错误:
Unknown rule: default
at Object.matchRule (/Developer/repos/bond/api/node_modules/waterline/node_modules/anchor/lib/match/matchRule.js:38:11)
at Anchor.to (/Developer/repos/bond/api/node_modules/waterline/node_modules/anchor/index.js:106:48)
at afterwards (/Developer/repos/bond/api/node_modules/waterline/lib/waterline/core/validations.js:229:41)
at /Developer/repos/bond/api/node_modules/async/lib/async.js:52:16
at Object.async.forEachOf.async.eachOf (/Developer/repos/bond/api/node_modules/async/lib/async.js:236:30)
at Object.async.forEach.async.each (/Developer/repos/bond/api/node_modules/async/lib/async.js:209:22)
at _eachValidation (/Developer/repos/bond/api/node_modules/waterline/lib/waterline/core/validations.js:189:11)
at /Developer/repos/bond/api/node_modules/async/lib/async.js:181:20
at Object.async.forEachOf.async.eachOf (/Developer/repos/bond/api/node_modules/async/lib/async.js:233:13)
at Object.async.forEach.async.each (/Developer/repos/bond/api/node_modules/async/lib/async.js:209:22)
at Validator.validate (/Developer/repos/bond/api/node_modules/waterline/lib/waterline/core/validations.js:144:9)
at /Developer/repos/bond/api/node_modules/waterline/lib/waterline/query/validate.js:42:25
at /Developer/repos/bond/api/node_modules/async/lib/async.js:718:13
at Immediate.iterate [as _onImmediate] (/Developer/repos/bond/api/node_modules/async/lib/async.js:262:13)
at processImmediate [as _immediateCallback] (timers.js:383:17)
我该如何解决这个问题?
我查看了 similar errors,但我的 none 个模型具有 default
属性,包括上面显示的 phone
模型。为什么会出现这种情况,我该如何解决?
解决方案
所以结果是 .update()
的 Waterline 调用改变了输入信息。我写了一个函数来创建或更新水线模型,当他们修复 auto updated_at issue 时,他们一定改变了传入的信息。
我以前的代码不起作用:
createOrUpdate: function(model, primary, data){
return model.update(primary, data)
.then(function updateCB(updated){
if (updated.length == 0){
return model.create(data) //data here had an added updated_at field
.then(function(created){
return created;
});
}
if (updated.length > 0){
updated = updated[0];
}
return updated;
});
}
我的新代码有效:
createOrUpdate: function(model, primary, data){
const data1 = JSON.parse(JSON.stringify(data)); // deep copy
const data2 = JSON.parse(JSON.stringify(data)); // deep copy
return model.update(primary, data1)
.then(function updateCB(updated){
if (updated.length == 0){
return model.create(data2)
.then(function(created){
return created;
});
}
if (updated.length > 0){
updated = updated[0];
}
return updated;
});
}
你不能调用更新:
updated_at: Sun Nov 27 2016 16:59:45 GMT-0800 (PST)
Updated_at是自动填充的字段。从请求中删除它(随叫随到)它会起作用。