javascript - 如何使用 sequelize bulkCreate 根据用户输入在数据库中插入多行
问题描述
我正在构建一个工作项目,它是一个输入表单,供用户将数据输入到我们的 postgres 数据库中。在某些表中,用户需要能够多次输入相同的项目 ID,但具有与之关联的不同值,最后两列的数据类型为 real。
例如,用户应该能够一次输入所有这些信息,而无需重新提交表单三次。
"project 1" "data-1" "data-a" 313 0
"project 1" "data-2" "data-b" 6282 130.5
"project 1" "data-3" "data-c" 4091 85
但是,当我在后端使用 bulkCreate 提交表单时,我得到了这些数据。
我希望数据分成三行,只有一条数据,就像上面的例子一样,而不是三行,对象包含三个数据条目。
{"project 1", "project 1", "project 1"} {"data-1", "data-1", "data-1"} {"data-a", "data-a", "data-a"} {NaN, NaN, NaN} {Nan, NaN, NaN}
{"project 1", "project 1", "project 1"} {"data-2", "data-2", "data-2"} {"data-b", "data-b", "data-b"} {NaN, Nan, Nan} {Nan, NaN, NaN}
{"project 1", "project 1", "project 1"} {"data-3", "data-3", "data-3"} {"data-c", "data-c", "data-c"} {NaN, Nan, Nan} {Nan, NaN, NaN}
这是我的控制器,我在 req.body 中的内容被发布了三次,在我的 html 中,数据被输入到一个表中,我有一个 jquery 函数,它在按钮单击时添加一个新的,以便用户可以添加更多数据到表格。因此,在测试表单时,我单击该按钮以在原始行的顶部添加两行以输入数据,这就是为什么每一行都是三个项目的对象。由于 req.body 的内容被插入了三次,我得到了三个单独的行。
createCostsHours: async (req, res) => {
const { project_id, imp_or_ann, category, cost, hours } = req.body;
try {
const costHours = await Prjt_costs_hours.bulkCreate([
{project_id, imp_or_ann, category, cost, hours},
{project_id, imp_or_ann, category, cost, hours},
{project_id, imp_or_ann, category, cost, hours},
]);
return res.redirect('/fundings')
} catch (error) {
console.error(error.message);
return res.status(500).json(error);
}
},
<form action='/costs_hours' method="POST">
<h1 class="text-center mb-3">Costs and Hours</h1>
<div class="card border-secondary w-100 text-light" style="background-color: #333f48">
<div style="background-color: #bf5700;" class="card-header text-end">
<button style="background-color: #333f48" class='btn text-light' id="addRow" type="button">Add another
row</button>
</div>
<div class="card-body w-100 text-end">
<table id="tableData" class="table text-light text-center mt-3">
<thead>
<tr>
<th scope="col">Project ID</th>
<th scope="col">Implementation or Annual</th>
<th scope="col">Category</th>
<th scope="col">Costs</th>
<th scope="col">Hours</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="input-group mb-3">
<input name="project_id" type="text" class="form-control">
</div>
</td>
<td>
<div class="input-group mb-3">
<div class="input-group mb-3">
<select name="imp_or_ann" class="form-select" id="inputGroupSelect01">
<option disabled selected>Choose...</option>
<option>Implementation</option>
<option>Annual</option>
</select>
</div>
</div>
</td>
<td>
<div class="input-group mb-3">
<div class="input-group mb-3">
<select name="category" class="form-select" id="inputGroupSelect01">
<option disabled selected>Choose...</option>
<option>EMO</option>
<option>Analysts</option>
<option>Maintenance</option>
<option>ETS</option>
<option>BOT</option>
<option>OtherUT</option>
<option>Materials</option>
<option>Non-UT Contract</option>
<option>Contingency</option>
</select>
</div>
</div>
</td>
<td>
<div class="input-group mb-3">
<input name="cost" type="text" class="form-control">
</div>
</td>
<td>
<div class="input-group mb-3">
<input name="hours" type="text" class="form-control">
</div>
</td>
</tr>
</tbody>
</table>
</div>
<button type="submit" style="background-color: #bf5700;" class="btn btn-warning text-light">Next</button>
</div>
</form>
解决方案
推荐阅读
- ios - 如何发布仅适用于 iPhone 而不是 iPad 的应用程序?
- javascript - 如何从 IE 11 中的事件中获取父元素的属性?
- docker - 更新主机中的 GPU 驱动程序后,旧的 docker 容器不可用(无 GPU)
- javascript - 如何强制 VS Code 允许我在我的 JavaScript 中输入纬度和经度?
- jquery - 如何调试原型jQuery函数
- kubernetes - 如何在 kubernetes 中解耦配置?
- java - 如何在 IMap 中使用 Hazelcast MapStore 数据
- python - KivyMD MDFlatButton 不可点击 & Kivy ScreenManager 不工作
- c++ - PCL 1.11 缺少文件/目录错误消息
- sql - 在 ('str1','str2','str3') 中使用 upper(string) 的奇怪 where 子句行为