2017-07-25 104 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對象的一部分。