Javascript 如何使用sequelize bulkCreate根据用户输入在数据库中插入多行
我正在为work建立一个项目,它是一个输入表单,用户可以将数据输入我们的postgres数据库。在某些表中,用户需要能够多次输入相同的项目ID,但与之关联的值不同,最后两列是real的数据类型 例如,用户应该能够一次在中输入所有信息,而无需分别三次重新提交表单Javascript 如何使用sequelize bulkCreate根据用户输入在数据库中插入多行,javascript,html,database,postgresql,sequelize.js,Javascript,Html,Database,Postgresql,Sequelize.js,我正在为work建立一个项目,它是一个输入表单,用户可以将数据输入我们的postgres数据库。在某些表中,用户需要能够多次输入相同的项目ID,但与之关联的值不同,最后两列是real的数据类型 例如,用户应该能够一次在中输入所有信息,而无需分别三次重新提交表单 "project 1" "data-1" "data-a" 313 0 "project 1" "data-2" "
"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函数,它添加了一个新的on按钮点击,这样用户就可以在表单中添加更多的数据。因此,在测试表单时,我单击该按钮,在原始行的顶部添加两行以输入数据,这就是为什么每行都是三个项的对象。由于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);
}
},
费用和时间
再加一个
一行
项目ID
实施或年度
类别
成本
小时
选择。。。
实施
年度
选择。。。
应急措施组织
分析员
维修
ETS
机器人程序
奥瑟特
材料
非UT合同
意外事故
下一个
<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>