2017-07-25 106 views
0

我有一个MySQL表使用这些设置:使用JSON插入到MySQL JSON格式栏的NodeJS

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文本:“无效的值。”在值(或列)的'位置1'[object object]中。

你能帮我吗?

谢谢。

回答

3

您正在发送一个名为'[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'}); 
     } 
     }); 
    }); 
+0

我现在正在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”:真 “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行 – malignois

-1

感谢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对象的一部分。