【问题标题】:How to insert multiple record of JSON that stored in an Array using mysql and knex js (Node js)?如何使用mysql和knex js(Node js)插入存储在数组中的多条JSON记录?
【发布时间】:2020-02-04 20:50:16
【问题描述】:

我有一个代码来存储 id_company、id_variablepoint 和使用 knex js(节点 js)和 mysql 对表“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

请帮帮我,谢谢

【问题讨论】:

  • 所以。究竟是什么问题?请求是一个数组?

标签: mysql node.js express knex.js


【解决方案1】:

根据 knex doc knex 'insert' 支持数组。所以你不需要担心你的场景。只需将请求传递给 knex insert。

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

【讨论】:

    猜你喜欢
    • 2016-02-16
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多