gpt4 book ai didi

node.js - 使用 Sequelize 与 Node 和 Express 批量插入

转载 作者:太空宇宙 更新时间:2023-11-04 00:09:51 25 4
gpt4 key购买 nike

寻找一种使用 Sequelize Model.bulkCreate 将事件批量插入数据库的方法。

不确定如何将事件从 View 引入路由函数,以便使用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) 让字段的名称类似于数组

<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 模式的正文解析器具有扩展选项,可以深入解析所有字段。您必须启用它。
确保 bodyParser 中间件附加了 extended: true :

 const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));

关于node.js - 使用 Sequelize 与 Node 和 Express 批量插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50358641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com