使用mysql模块为Nodejs存储JSON

我需要在这个表的'data'列中存储一个JSON:

在这里输入图像说明

我试图存储一个JSON,但我收到错误,可能是由于引用…

connection.connect(); var query = 'INSERT INTO Forecasts (data, createdAt, updatedAt, OrchardId) VALUES ?'; connection.query(query, [['{"id": 1}', '2017-07-06 16:08:19','2017-07-06 16:08:19',3]],function (error, results, fields) { if (error) throw error; console.log('The solution is: ', results[0].solution); }); connection.end(); 

OUTPUT

$ npm开始

revo-weather@1.0.0 start / Users / giovannimarino / Desktop / revo-weather node index.js

{“id”:1} INSERT INTO预测(data,createdAt,updatedAt,OrchardId)VALUES(“gio”,“2017-07-06 16:08:13”,“2017-07-06 16:08:13” ,3)MacBook-Pro-di-Giovanni:revo-weather giovannimarino $ npm start

revo-weather@1.0.0 start / Users / giovannimarino / Desktop / revo-weather node index.js

/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/protocol/Parser.js:79 throw err; / /重新思考非MySQL错误^

错误:ER_PARSE_ERROR:您的SQL语法中有错误; 检查与您的MySQL服务器版本相对应的手册,在“{\”id \“:1}','2017-07-06 16:08:19','2017-07-06 16 :Query:Query.Sequence._packetToError(/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)第1行08:19',3'。 ErrorPacket(/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)在Protocol._parsePacket(/用户/ giovannimarino /桌面/ revo-weather / node_modules / mysql /lib/protocol/Protocol.js:280:23)在Protocol.write(Parser.write(/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/protocol/Parser.js:75:12) /Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/protocol/Protocol.js:39:16)。 (events.js:210:7)处的emitOne(events.js:115:13)处(/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/Connection.js:103:28)在可读的AddChunk(_stream_readable.js:239:11)上的addChunk(_stream_readable.js:252:12) (/Users/giovannimarino/Desktop/revo-weather/node_modules/mysql/lib/Connection.js:141:48)在giovannimarino / Desktop / revo-weather / node_modules / mysql / lib / protocol / 208:25)在对象。 (/Users/giovannimarino/Desktop/revo-weather/index.js:19:12)在Module._compile(module.js:569:30)在Object.Module._extensions..js(module.js:580:10) )在Function.Module.runMain(module.js:458:3)处的Function.Module._load(module.js:458:3)处的Module.load(module.js:503:32)处的tryModuleLoad :605:10)在启动时(bootstrap_node.js:158:16)npm ERR! 代码ELIFECYCLE npm ERR! errno 1 npm ERR! revo-weather@1.0.0 start: node index.js npm ERR! 退出状态1 npm ERR! npm ERR! 在revo-weather@1.0.0启动脚本失败。 npm ERR! 这可能不是npm的问题。 上面可能还有额外的日志输出。

npm ERR! 这个运行的完整日志可以在下面find:npm ERR!
/Users/giovannimarino/.npm/_logs/2017-07-14T14_16_03_166Z-debug.log

JSON.strigify你的JSON对象,然后坚持下去。 在撤回数据的同时,您可以再次parsing数据。

 connection.connect(); var query = 'INSERT INTO Forecasts (data, createdAt, updatedAt, OrchardId) VALUES ?'; connection.query(query, [[JSON.stringify('{"id": 1}'), '2017-07-06 16:08:19','2017-07-06 16:08:19',3]],function (error, results, fields) { if (error) throw error; console.log('The solution is: ', results[0].solution); }); connection.end(); 

你可以简单地使用:

 JSON.stringify(data)