我正在尝试从bootstrap conf将初始数据加载到sails应用程序(sails v1.0.2)中,但它失败并出现错误:
error: UsageError: Invalid initial data for new record.
Details:
Missing value for required attribute `agentType`. Expected a string, but instead, got: undefined
模型:
module.exports = {
attributes: {
agentType: {
type: 'string',
required: true
},
amount: {
type: 'number',
defaultsTo: 0
},
application: {
model: 'application'
}
},
};
我的bootstrap文件:
const agents = [
{
agentType: 'Java App Agent',
amount: 30
}, {
agentType: '.NET App Agent',
amount: 18
}, {
agentType: '.NET Machine Agent',
amount: 33
}, {
agentType: 'Standalone Machine Agent',
amount: 46
}];
module.exports.bootstrap = function(done) {
agents.forEach(agent => {
Agent.findOrCreate({
agentType: agent.agentType,
amount: agent.amount
})
.exec((error, agent) => {
if (error) sails.log.error(error);
sails.log.info('Created fixture agent', agent)
})
});
done()
};
我正在使用“sails lift --drop”命令启动sails应用程序。
请告诉我我做错了什么。
提前致谢!
正如Gitter的@texh指出:
https://sailsjs.com/documentation/reference/waterline-orm/models/find-or-create
module.exports.bootstrap = function(done) { agents.forEach(agent => { Agent.findOrCreate(agent, agent) .exec((error, agent) => { if (error) sails.log.error(error); sails.log.info('Created fixture agent', agent) }) }); done() };
如果找不到记录,则必须传递搜索条件和初始值