SailsJS conf.bootstrap无法插入初始数据

时间:2018-09-17 14:47:13

标签: sails.js

我正在尝试从引导程序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'
    }
  },
};

我的引导文件:

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”命令启动帆应用程序。

请告知我我做错了。

谢谢!

1 个答案:

答案 0 :(得分:0)

正如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()
};
     

您必须通过搜索条件和初始值才能设置   找不到记录