gpt4 book ai didi

mysql - 如何使用 mysql 和 knex js (Node js) 插入存储在数组中的多个 JSON 记录?

转载 作者:行者123 更新时间:2023-11-29 15:11:34 31 4
gpt4 key购买 nike

我有一个代码来使用 knex js (node js) 和 mysql 存储 id_company、id_variablepoint 和表“answer”的答案

router.post("/insert_answer", async (req, res, next) => {
const id_company = req.body.id_company;
const id_variablepoint = req.body.id_variablepoint;
const answer = req.body.answer;

try{
const tambah = await knex("answer").insert([{id_company:id_company, id_variablepoint:id_variablepoint, answer:answer}])
res.json({
"data":tambah
})
}catch(e){
const error = new Error ("ERROR: "+e);
next(error);
}
});

我在存储如下所示的请求时遇到问题,因为它存储在数组中

[
{
"id_company":2,
"id_variablepoint":57,
"answer":"choose"
},
{
"id_company":2,
"id_variablepoint":49,
"answer":"choose"
}
]

我的数据库结构如下所示:

db structure

请帮帮我,谢谢

最佳答案

根据 knex doc knex“插入”支持数组。所以你不需要担心你的情况。只需将请求传递到 knex insert 即可。

const tambah = wait knex("answer").insert(req.body);

关于mysql - 如何使用 mysql 和 knex js (Node js) 插入存储在数组中的多个 JSON 记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60026253/

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