【问题标题】:Insert JSON into MySQL JSON column format using NodeJS使用 NodeJS 将 JSON 插入 MySQL JSON 列格式
【发布时间】:2017-12-31 01:20:18
【问题描述】:

我有一个带有这些设置的 Mysql 表:

    CREATE TABLE `WorkOrders` (
  `workorder_id` int(11) NOT NULL AUTO_INCREMENT,
  `intertype_id` int(11) NOT NULL,
  `equipment_id` int(11) NOT NULL,
  `reason_id` int(11) NOT NULL,
  `requester_id` int(11) NOT NULL,
  `workorder_creationdatetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `phase_id` int(11) NOT NULL,
  `criticality_id` int(11) NOT NULL,
  `workorder_targetdate` date DEFAULT NULL,
  `workorder_enddatetime` datetime DEFAULT NULL,
  `workorder_content` json DEFAULT NULL,
  PRIMARY KEY (`workorder_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

“workorder_content”列具有 JSON 数据类型。

然后我有一个使用 NodeJS api 链接到数据库的 AngularJS + Bootstrap。

我正在尝试使用该脚本将数据插入到我的表中:

connection.acquire(function(err, con) {
      con.query('INSERT INTO WorkOrders SET ?', workorder.workorder.int, function(err, result) {
        con.release();
        console.log(err);
        if (err) {
          res.send({status: 1, message: 'WorkOrder creation failed'});
        } else {
          res.send({status: 0, message: 'WorkOrder created successfully'});
        }
      });
    });

这是 workorder.workorder.int 包含的内容:

{ phase_id: 1,
  intertype_id: '14',
  reason_id: '1',
  equipment_id: '2',
  requester_id: '24',
  criticality_id: '29',
  workorder_content: 
   { adresse_client: 'sefsdf',
     type_machin: '1',
     type_truc: '8',
     truc_cocher: true,
     truc_radio: '11',
     date_debut: '12/07/2017' } }

我在 NodeJS 控制台中遇到的错误是: { 错误:UNKNOWN_CODE_PLEASE_REPORT:无效的 JSON 文本:“无效值。”在值(或列)“[object Object]”中的位置 1。

你能帮帮我吗?

谢谢。

【问题讨论】:

    标签: javascript mysql angularjs json node.js


    【解决方案1】:

    您正在发送一个名为“[object Object]”的字符串。到 MySQL 而不是对象。请尝试以下操作:

    connection.acquire(function(err, con) {
          con.query('INSERT INTO WorkOrders SET ?', JSON.stringify(workorder.workorder.int), function(err, result) {
            con.release();
            console.log(err);
            if (err) {
              res.send({status: 1, message: 'WorkOrder creation failed'});
            } else {
              res.send({status: 0, message: 'WorkOrder created successfully'});
            }
          });
        });
    

    【讨论】:

    • 我现在在 workorder.workorder.int 中有这个:{"phase_id":1,"intertype_id":"14","re​​ason_id":"1","equipment_id":"2" ,"requester_id":"24","criticality_id":"29","workorder_content":{"adresse_client":"sefsdf","type_machin":"1","type_truc":"8","truc_cocher": true,"truc_radio":"11","date_debut":"12/07/2017"}} 但我有这个错误:错误:ER_PARSE_ERROR:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 ''{\"phase_id\":1,\"intertype_id\":\"14\",\"reason_id\":\"1\ 附近使用的正确语法",\"equipment_id\":\"' 在第 1 行
    【解决方案2】:

    感谢sidewinder,我找到了解决方案。

    这里是:

    workorder.workorder.int.workorder_content = JSON.stringify(workorder.workorder.ext);
    
        connection.acquire(function(err, con) {
          con.query('INSERT INTO `WorkOrders` SET ?', workorder.workorder.int, function(err, result) {
            con.release();
            console.log(err);
            if (err) {
              res.send({status: 1, message: 'WorkOrder creation failed'});
            } else {
              res.send({status: 0, message: 'WorkOrder created successfully'});
            }
          });
        });
    

    因此需要对将插入 MySQL 中 JSON 列的部分进行字符串化,而不是对整个 JSON 对象进行字符串化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-15
      • 2022-08-14
      • 2018-03-02
      • 2011-05-14
      • 2019-11-30
      • 2014-02-26
      • 2016-10-10
      • 1970-01-01
      相关资源
      最近更新 更多