使用Sequelize with Node和Express批量插入

时间:2018-05-15 20:24:12

标签: node.js express sequelize.js

寻找使用Sequelize Model.bulkCreate批量插入数据库的方法。

不确定如何将视图中的活动带到路径功能,以便使用bulkCreate插入它们。现在我只插入一个活动。

查看:

<form action="/create" method="POST">
  <div class="form-group">
    <label>Plan Name</label>
    <input type="text" name="plan_name" placeholder="Plan Name">
  </div>
  <div class="form-group">
    <label>Description</label>
    <input type="text" name="description" placeholder="Description">
  </div>
  <div class="form-group">
    <label>Activity</label>
    <input type="text" name="activity_name" placeholder="Activity">
    <label>Comment</label>
    <input type="text" name="comment" placeholder="Comment">
    <input type="button" id="add" class="btn btn-success" value="Add">
  </div>
  <div class="form-group" id="new">
  </div>
    <input type="hidden" name="_csrf" value="{{csrfToken}}">
    <input type="submit" class="btn btn-success" value="Submit">
</form>
<script type="text/javascript">
  $('#add').click(function() {
    let activity = $('<div class="form-group"><label>Activity</label><input type="text" name="activity_name" placeholder="Activity"><label>Comment</label><input type="text" name="comment" placeholder="Comment"></div>');
    $('#new').append(activity);
  });
</script>

路线:

  app.post('/create', (req, res) => {
    const user_id = req.user.id;
    const data = {
      plan_name: req.body.plan_name,
      description: req.body.description,
      userId: user_id
    };
    PlanModel.Plan.create(data).then(plan => {
      const activity = {
        activity_name: req.body.activity_name,
        comment: req.body.comment,
        plans_id: plan.id
      };
      ActivityModel.Activity.create(activity);
      res.redirect('/');
    });
  });

1 个答案:

答案 0 :(得分:0)

1)使字段具有类似数组的名称

<div class="activities">

  <div class="form-group">
    <label>Activity</label>
    <input type="text" name="activities[0][name]" placeholder="Activity">
    <label>Comment</label>
    <input type="text" name="activities[0][comment]" placeholder="Comment">
  </div>

  <div class="form-group">
    <label>Activity</label>
    <input type="text" name="activities[1][name]" placeholder="Activity">
    <label>Comment</label>
    <input type="text" name="activities[1][comment]" placeholder="Comment">
  </div>

</div>

<div class="form-group>
  <input type="button" id="add" class="btn btn-success" value="Add">
</div>

2)在服务器端,只需获取req.body.activities并生成对象数组并调用bulkCreate

app.post('/create', async (req, res) => {
  try {
    const data = {
      plan_name: req.body.plan_name,
      description: req.body.description,
      userId: req.user.id
    };
    const plan = await PlanModel.Plan.create(data);


    if (req.body.activities && Array.isArray(req.body.activities)) {
      const activities = req.body.activities.map(
        activity => {
          return {
            activity_name: activity.name,
            comment: activity.comment,
            plans_id: plan.id
          }
        });
      await ActivityModel.Activity.bulkCreate(activities);
    }

    res.redirect('/');
  }
  catch(error) {
    res.status(500).send(error);
  }
});

3)由于urlencoded模式的body解析器具有扩展选项,可以深入解析所有字段。你要启用它。
   确定 <{1}}附带的 bodyParser 中间件:

extended: true