我有一个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应用程序接口将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控制台中看到的错误是:{ Error: UNKNOWN_CODE_PLEASE_REPORT: Invalid JSON text:"Invalid value.“在值(或列)“Object Object”中的位置%1。
你能帮帮我吗?
谢谢。
发布于 2017-07-25 16:29:21
您正在发送一个名为“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'});
}
});
});发布于 2017-07-25 18:04:43
多亏了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对象进行字符串化。
https://stackoverflow.com/questions/45297806
复制相似问题